in reply to HTML table with HTML::Template and loop in a loop

Well there are some good explainations here but I'm not sure anyone got at your core question. Which is "What type of data structure does HTML::Template take?"

At the most basic level HTML::Template takes a hash. If you have a hash of key value pairs that all appear in the template then you can simply pass it the param call the hash and it will work.

The <tmpl_loop> structure takes a reference to an array of hash references. And you can have loops inside your loops.

So here's how you can make @data into @loop_data, I'm going to avoid using map so that you can get a clearer idea of where the data is going:

my @loop_data = (); foreach my $array_ref(@data) { my @inner_loop = (); foreach my $element (@$array_ref) { push(@inner_loop,{var1=>$element}); } push(@loop_data,{loop2=>\@inner_loop}); }

Hope that helps... I'm going to post this and the try to write some code that does it with map I can't think of it off the top of my head right now.

***UPDATE***
Here's some code to do it with map

my @loop_data = map { {loop2=> [map { {var1=>$_ } } @$_ ] } } @data;

***UPDATE 2 ***
Fixed the bug. :) thanks fireartist

Chris

Lobster Aliens Are attacking the world!

Replies are listed 'Best First'.
Re: Re: HTML table with HTML::Template and loop in a loop
by fireartist (Chaplain) on Sep 23, 2002 at 15:09 UTC
    Thanks cfreak, this was helpful.

    small typo though,
    my @loop_data = (); foreach my $array_ref(@data) { my @inner_loop = (); foreach my $element (@$_) { push(@inner_loop,{var1=>$element}); } push(@loop_data,{loop2=>\@inner_loop}); }
    In the 4th line, (@$_) should read (@$array_ref).