You'll need to make nested loops, which are kind of a pain in HTML::Template. The data structure is fairly straight-forward, but generating it usually takes some nested maps. The idea is to basically make an array slice of each output column using the @selection array of indices. Here, I've generated nested row and column loops:
use HTML::Template; my @data = ( [ qw/a0 a1 a2 a3 a4 a5/ ], [ qw/b0 b1 b2 b3 b4 b5/ ], [ qw/c0 c1 c2 c3 c4 c5/ ], [ qw/d0 d1 d2 d3 d4 d5/ ] ); my @col_name = qw/ col0 col1 col2 col3 col4 col5 /; my @selection = (1, 4, 5); my $tmpl = HTML::Template->new( filehandle => \*DATA ); my @headers = map {{ title => $_ }} @col_name[ @selection ]; my @rows = map {{ cols => [ map {{ data => $_ }} @$_[ @selection ] +] }} @data; $tmpl->param( headers => \@headers, rows => \@rows ); print $tmpl->output; __DATA__ <table border=1> <tr> <TMPL_LOOP headers><th><TMPL_VAR title></th></TMPL_LOOP> </tr> <TMPL_LOOP rows> <tr> <TMPL_LOOP cols><td><TMPL_VAR data></td></TMPL_LOOP> </tr> </TMPL_LOOP> </table>

which produces this output:

col1col4col5
a1a4a5
b1b4b5
c1c4c5
d1d4d5

If you're getting this data from a database, though, you can probably let the database handle the slicing of each row... either by using selectall_arrayref and its Slice option, or by just listing the required columns in the query.

blokhead


In reply to Re: Sending data for LOOP in HTML::Template by blokhead
in thread Sending data for LOOP in HTML::Template by Steny

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.