Here is my take on the problem, but first i gotta share this with you. Take a list of numbers and break them up into a two dimensional array, each inner array contains, say 4 elements. Here was my first naive try:
use strict; use Data::Dumper; my $i = 0; my (@tab,@row); for (0..10) { unless ($i % 4 or $i == 0) { push @tab,[@row]; @row = (); } push @row,$_; $i++; } push @tab,[@row]; print Dumper \@tab;
In a nutshell, we iterate through the list. Unless we have reached the 4th element, we store that element in another transitory list. If we have reached the 4th element, we store that list into our outer array. This was the logic i started with trying to solve your problem. It is rather inelegant as i have to push that final transitory array after the loop is finished - and that's what bit me when i applied it to this HTML::Template problem.

Now, consider this approach instead:

use strict; use Data::Dumper; my ($i,$j) = (0,0); my $tab; for (0..10) { push @{$tab->[$j]},$_; $j++ unless ++$i % 4; } print Dumper $tab;
Wow, so much simpler. This takes advantage of Perl's auto-vivification abilities. Instead of depending upon different states, we just do it - and we don't have to worry about clearing a transitory array, but we do have an extra counter for iteration. I'll take that extra counter for ease of programming.

So, here is my solution to this larger problem at hand. You will have to make modifications to suite your needs, but this should show you how to do it. I ran this code from my public_html directory on my images directory. That directory contained 3 subdirs with a different numbers of images in each one. I also bundled the template file into the code via the DATA filehandle.

use strict; use HTML::Template; use File::Basename; my $row_limit = 5; my $image_dir = 'images'; my %ok_ext = map { $_ => 1 } qw(jpg gif png); my $data = do {local $/; <DATA>}; my $template = HTML::Template->new( scalarref => \$data, ); my @dir_row; while(my $dir = <$image_dir/*>) { next unless -d $dir; my ($i,$j) = (0,0); my $dir_row = {dir => $dir}; while (my $full = <$dir/*>) { my ($file,$ext) = (fileparse($full,keys %ok_ext))[0,2]; next unless $ok_ext{$ext}; push @{$dir_row->{file_row}->[$j]->{images}}, { filename => $full, alt => $file }; $j++ unless ++$i % $row_limit; } push @dir_row, $dir_row; } $template->param( title => 'Images', dir_row => \@dir_row, ); print $template->output; __DATA__ <html> <head> <title><tmpl_var title></title> </head> <body> <center> <tmpl_loop dir_row> <table border="1"> <caption><tmpl_var dir></caption> <tmpl_loop file_row> <tr> <tmpl_loop images> <td><img src="<tmpl_var filename>" alt="<tmpl_var alt>"> </td> </tmpl_loop> </tr> </tmpl_loop> </tmpl_loop> </table> </center> </body> </html>

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

In reply to (jeffa) Re: Structure for nested html::template loops by jeffa
in thread building nested data structure / Structure for nested html::template loops by LTjake

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.