Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
How about something like this? It produces the correct output for me, although you'll probably want the hash keys sorted some way. This may be a little sloppy for some folks, because it uses lots of auto-vivification to create the deep @tables structure. I don't mind, though.
use HTML::Template; my $template = qq{ <TMPL_LOOP NAME=TABLE> <table border=1> <tr><th>key</th> <TMPL_LOOP NAME=KEYCELL> <td><TMPL_VAR NAME=KEY></td> </TMPL_LOOP> </tr><tr><th>value</th> <TMPL_LOOP NAME=VALCELL> <td><TMPL_VAR NAME=VAL></td> </TMPL_LOOP> </tr> </table> </TMPL_LOOP> }; my %somehash = map(("key$_" => "val$_"), 1..15); my $maxwidth = 6; my @tables = (); my $num_items = 0; for (keys %somehash) { my $table_num = int($num_items++ / $maxwidth); push @{$tables[$table_num]{keycell}}, { key => $_ }; push @{$tables[$table_num]{valcell}}, { val => $somehash{$_} }; } my $template = HTML::Template->new( scalarref => \$template ); $template->param( TABLE=>\@tables ); print $template->output;

-------------
This produces the output (keys are unsorted, as you can see):

key key7 key8 key9 key10 key11 key12
value val7 val8 val9 val10 val11 val12
key key13 key14 key1 key15 key2 key3
value val13 val14 val1 val15 val2 val3
key key4 key5 key6
value val4 val5 val6

I'd be interested to see if any masterful monks can reduce the for loop to a one-liner map() statement (or a two-liner if the keycell and valcell loops must be separated). Hope this helps, and good luck. (BTW, I modified your HTML template a little bit -- added indentation, border=1, and s/key/value/ in 7th line)

Update: to fit the specifications of your original question, you should change $maxwidth to 5, as you wanted a maximum of 6 cells including the <th> tags.


In reply to Re: Nesting <TMPL_LOOP> in HTML::Template by blokhead
in thread Nesting <TMPL_LOOP> in HTML::Template by rattusillegitimus

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-19 02:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found