l i n u x - u s e r s - g r o u p - o f - d a v i s
L U G O D
 
Next Meeting:
August 5: Social gathering
Next Installfest:
TBD
Latest News:
Jul. 4: July, August and September: Security, Photography and Programming for Kids
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.code=00001
item1.name=Hot Cocoa Mix
item1.price=$0.59
item1.qty=1

item2.code=00345
item2.name=Milk
item2.price=$1.00
item2.qty=1


And then in my template do something like:

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

:)

-bill!
overly proud, and starving for lunch


LinkedIn
LUGOD Group on LinkedIn
Sign up for LUGOD event announcements
Your email address:
facebook
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:
O'Reilly and Associates
For numerous book donations.