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!

In reply to Re: HTML table with HTML::Template and loop in a loop by cfreak
in thread HTML table with HTML::Template and loop in a loop by fireartist

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.