l i n u x - u s e r s - g r o u p - o f - d a v i s
Next Meeting:
July 7: Social gathering
Next Installfest:
Latest News:
Jun. 14: June LUGOD meeting cancelled
Page last updated:
2001 Dec 30 17:08

The following is an archive of a post made to our 'vox-tech mailing list' by one of its subscribers.

Report this post as spam:

(Enter your email address)
[vox-tech] Perl "Template" module and dynamic arrays of hashes in a hash
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[vox-tech] Perl "Template" module and dynamic arrays of hashes in a hash

WHEW!  I just bonked my head a few times over this, and got chunks of help
here and there from the #perl IRC channel.

Say you have a hash you're going to send to the award-winning "Template"
Perl module.  Say this hash is based on "token=value" pairs coming in
from STDIN.

Finally, say your template uses some of Template's "FOREACH" directive
so that it can act upon arrays?

Well!  Here's how you fill in a particular chunk of your hash with a
little hash in a piece of your array:

    ${$hash{$array}}[$index] { $label } = $value;

$hash is the thing being sent to Template's "process" method.
(Most of the items in this hash are simply things like {"Name" => "Bill"} )

$array is the label of a thing to iterate on within your template.
(It's the thing you mention in your "FOREACH" directive)

$index is a number saying _where_ in the array something is to go.
(That way you can have a number of array items and keep their hashes
stuck together)

$label is the name part of the hash item in the array $array.

And finally, $value is the value to store.

WHEW!!!  Complicated, huh?

But, it lets me do stuff like:

item1.name=Hot Cocoa Mix


And then in my template do something like:

[% FOREACH i = item %]
Code = [% i.code %]
Name = [% i.name %]
Price = [% i.price %]
Qty = [% i.qty %]
[% END %]


overly proud, and starving for lunch

LUGOD Group on LinkedIn
Sign up for LUGOD event announcements
Your email address:
LUGOD Group on Facebook
'Like' LUGOD on Facebook:

Hosting provided by:
Sunset Systems
Sunset Systems offers preconfigured Linux systems, remote system administration and custom software development.

LUGOD: Linux Users' Group of Davis
PO Box 2082, Davis, CA 95617
Contact Us

LUGOD is a 501(c)7 non-profit organization
based in Davis, California
and serving the Sacramento area.
"Linux" is a trademark of Linus Torvalds.

Sponsored in part by:
Appahost Applications
For a significant contribution towards our projector, and a generous donation to allow us to continue meeting at the Davis Library.