In this case, you want to slice across the arrays -- pick all the ones, then all the twos, etc. So your structure must be converted slightly, basically just swap logical rows with columns in your representation. To determine the structure, remember that all named items in your template must correspond to a hash key in the structure, and that loops correspond to array references. This is how you would logically build up your %loop_data structure, starting with the outer loop and moving inwards:
<!-- TMPL_LOOP NAME=loop1 --> # means you must have a hash key 'loop1' with an array ref as its valu +e # in the hash. %loop_data = ( loop1 => [ ..something.. ] ); <!-- TMPL_LOOP NAME=loop2 --> # another loop, so each of these 'somethings' must have a hash key loo +p2 # pointing to a value of some array ref %loop_data = ( loop1 => [ { loop2 => [ ..something.. ] }, { loop2 => [ ..something.. ] }, ... ] ); <!-- TMPL_VAR NAME=var1 --> # now each of these somethings must contain a hash key var1 pointing t +o # some scalar data. %loop_data = ( loop1 => [ { loop2 => [ { var1 => 'data'} ] }, { loop2 => [ { var1 => 'otherdata'} ] }, ... ] );
You may find it useful to rename the structure like this:
%loop_data = ( rows => [ { cols => [ { data => 'data'} ] }, { cols => [ { data => 'otherdata'} ] }, ... ] );
You can either build up your original structure in this way, or use something like the following to convert to this new structure:
#!/usr/bin/perl -wT use strict; use HTML::Template; my $html = do { local $/; <DATA> }; my $template = HTML::Template->new(scalarref => \$html); my @data = ( [1, 2, 3, 4, 5], ['one', 'two', 'three', 'four', 'five'], ['ein', 'zwei', 'drei', 'veir', 'funf'], ['hana', 'dool', 'set', 'net', 'dasut'], ['yi', 'er', 'san', 'si', 'wu'], ); my %loop_data; foreach my $array_num (0 .. $#data) { my $array_ref = $data[$array_num]; foreach my $array_index (0 .. $#{@$array_ref}) { $loop_data{rows}[$array_index]{cols}[$array_num]{data} = $arra +y_ref->[$array_index]; } } $template->param(\%loop_data); print $template->output; exit; __DATA__ <html> <body> <table> <!-- TMPL_LOOP NAME=rows --> <tr> <!-- TMPL_LOOP NAME=cols --> <td><!-- TMPL_VAR NAME=data --></td> <!-- /TMPL_LOOP --> </tr> <!-- /TMPL_LOOP --> </table> </body> </html>
This produces the correct output for me.

blokhead


In reply to Re: HTML table with HTML::Template and loop in a loop by blokhead
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.