Re: [htmltmpl] (newbie) How to populate an array of hash references one by one?
Brought to you by:
samtregar
From: Jayson P. <JP...@je...> - 2003-11-24 20:05:38
|
Attempt #1-3 are missing a comma between your array and your list. Solution, use a comma. Attempt #4 is assigning an array reference as the first element of an array and then you are using a reference to that array. Solution, use () instead of [] in your assignment. Attempt #5 is a hashref not an array ref. Solution, put all your hash elements into an array. Attempt #6 is a hash itself, not even an array. I'd recommend using #1 with a comma like so (push @test, { name=3D>"foo",value=3D>"blitz" };) and you should be working. --Jayson Ken Corey <ke...@ke...> Sent by: htm...@li... 11/24/03 02:55 PM To: htm...@li... cc: Subject: [htmltmpl] (newbie) How to populate an array of hash references one by one=3F Hi All, I'm playing with HTML::Template, and I've run into something that should be easy, but I don't see how it can work. The docs say to pass many elements for a loop, you do this: $template->param( loopvar =3D> [ { name=3D>"foo",value=3D>"baloney" }, { name=3D>"bar",value=3D>"tiple" }, { name=3D>"blah",value=3D>"mulroney" }, ], ); So, I'm using anonymous array and hash operators to make an anonymous array of hash references. (page 246, "The anonymous array composer", camel book). That's great, but what if you don't know your content at programming time=3F {Just for context, I'm using perl 5.6.1, HTML::Template 2.6, with "use strict"}. Ideally, I want to add a line at a time to that array, and then pass that whole array to the param. So, I tried various things: # attempt 1 my @test =3D (); push @test { name=3D>"foo",value=3D>"blitz" }; # Gets "Global symbol "%test" requires explicit package name at ...[push line]" !=3F!=3F! there is no %test. # attempt 2 my @test =3D (); push @test \{ name=3D>"foo",value=3D>"blitz" }; # Gets "Backslash found where operator expected at ...[push line]" # attempt 3 my @test =3D (); push @test [ { name=3D>"foo",value=3D>"blitz" } ]; # Gets "Type of arg 1 to push must be array (not array slice) at ...[push line]" [more stuff along the same lines that didn't work...] *grrr* Okay, says I, I'll work on something else and wait for the muse to strike. I decide I'll figure out how to pass an array into a param... # attempt 4 my @test =3D [ { a=3D>"b",c=3D>"d" }, { e=3D>"f",g=3D>"h" }, ]; $template->param(itemloop =3D> \@test); # Gets "HTML::Template->output() : fatal error in loop output : HTML::Template->param() : Single reference arg to param() must be a has-ref! You gave me a ARRAY. at ..." # attempt 5 my %test; $test{"a"}=3D"b"; $test{"c"}=3D"d"; $test{"e"}=3D"f"; $template->param(itemloop =3D> \%test); # Gets "HTML::Template::param() : attempt to set parameter 'itemloop' with a scalar - parameter is not a TMPL=5FVAR! at ..." # attempt 6 my %test; $test{"a"}=3D"b"; $test{"c"}=3D"d"; $test{"e"}=3D"f"; $template->param(itemloop =3D> %test); # Gets "You gave me an odd number of parameters to param()! at ..." Ach. So, no better luck. Clearly there's some simple skullduggery I'm missing here. Can anyone enlighten me=3F Pointers to documentation would be fine, though the HTML::Template page and the perldoc for HTML::Template didn't help too much already. -Ken ------------------------------------------------------- This SF.net email is sponsored by: SF.net Giveback Program. Does SourceForge.net help you be more productive=3F Does it help you create better code=3F SHARE THE LOVE, and help us help YOU! Click Here: http://sourceforge.net/donate/ =5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F= =5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F=5F Html-template-users mailing list Htm...@li... https://lists.sourceforge.net/lists/listinfo/html-template-users Jefferies archives and reviews outgoing and incoming e-mail. It may be prod= uced at the request of regulators or in connection with civil litigation. Jefferies accepts no liability for any errors or omissions arising as a resu= lt of transmission. Use by other than intended recipients is prohibited. |