I wish to use HTML::Template to create the columns and rows of a table, using a LOOP inside of a LOOP.
I've read the POD, and jeffa's excellent HTML::Template Tutorial, but the problem is that I can't understand how his various map's are creating the required data structures for HTML::Template.

I've created an Array of Array's full of data to put into a HTML table, but can't figure out how to get it into HTML::Template.

I have an array,
@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'], );
and would like to output a HTML table like so,
1 one ein hana yi 2 two zwei dool er 3 three drei set san 4 four veir net si 5 five funf dasut wu
I know how to manipulate the @data array to print it out like that,
use strict; use warnings; my @array = ([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 @sorted; my $i = 0; for (@array) { for ( @{$_} ) { push @{$sorted[$i]}, $_; $i ++; } $i = 0; } for (@sorted) { for ( @{$_} ) { print $_, ' ' } print $/ }
outputs
1 one ein hana yi 2 two zwei dool er 3 three drei set san 4 four veir net si 5 five funf dasut wu
...so I do know how to manipulate the data, I just don't understand exactly what data structure HTML::Template requires.

If anybody gives a solution using map, I would really appreciate an explanation of what the map is doing.

Here is the code I have so far, including the HTML template:
#!/usr/bin/perl -wT use strict; use CGI; $CGI::DISABLE_UPLOADS = 1; use CGI::Carp qw/fatalsToBrowser/; use HTML::Template; use vars qw/ $q $template $html @loop_data @data /; $q = new CGI; $html = do { local $/; <DATA> }; $template = HTML::Template->new(scalalref => \$html); @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'], ); ### DO SOMETHING HERE TO CREATE @loop_data ! $template->param(loop1 => \@loop_data); print $q->header; print $template->output; exit; __DATA__ <html> <body> <table> <!-- TMPL_LOOP NAME=loop1 --> <tr> <!-- TMPL_LOOP NAME=loop2 --> <td><!-- TMPL_VAR NAME=var1 --></td> <!-- /TMPL_LOOP --> </tr> <!-- /TMPL_LOOP --> </table> </body> </html>
Thanks!

In reply to 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.