As a follow-up to vladb's answer to my previous question here's next my question:

I've implemented vladb's code in a little different way, however using the same push calls, and my structure doesn't come out as I want it.

My code is as follows:
opendir(DIRHANDLE, $self->param('photos_dir')) or die "couldn't open m +ain directory: $!"; $dir_row_count = 0; $file_count = 0; $row_limit = 5; while (defined($filename = readdir(DIRHANDLE))) { next if $filename =~ /^\.\.?$/ || !(-d $filename); push @$dir_list, { dir => $filename, dir_row => [] }; opendir(SUBDIRHANDLE, "$filename") or die "couldn't open directory + $filename: $!"; my @pictures = grep { /\.(?:png|gif|jpg)$/ } readdir SUBDIRHAN +DLE; closedir(SUBDIRHANDLE); push @{$dir_list->[scalar @$dir_list - 1]{dir_row}}, { file_row => + [] }; $file_count = 0; foreach (@pictures) { unless ($file_count % $row_limit) { push @{$dir_list->[scalar @$dir_list - 1]{dir_row}}, { fil +e_row => [] }; } push @{$dir_list->[scalar @$dir_list - 1]{dir_row}[$dir_row_co +unt]{file_row}}, { filename => "$filename/$_", alt => $_ }; $file_count++; } $dir_row_count++; }
Using data::dumper i get:
$VAR1 = [ { 'dir' => '50s', 'dir_row' => [ { 'file_row' => [ { 'alt' => 'band_table.jp +g', 'filename' => '50s/band +_table.jpg' }, { 'alt' => 'cake_t2.jpg', 'filename' => '50s/cake +_t2.jpg' }, { 'alt' => 'cake-t.jpg', 'filename' => '50s/cake +-t.jpg' }, { 'alt' => 'an_logo.jpg', 'filename' => '50s/an_l +ogo.jpg' }, { 'alt' => 'gabe-bpm.jpg' +, 'filename' => '50s/gabe +-bpm.jpg' }, { 'alt' => 'cake_enter.jp +g', 'filename' => '50s/cake +_enter.jpg' } ] }, { 'file_row' => [] }, { 'file_row' => [] } ] }, { 'dir' => '60s', 'dir_row' => [ { 'file_row' => [] }, { 'file_row' => [ { 'alt' => 'cake_cdsleeve +.jpg', 'filename' => '60s/cake +_cdsleeve.jpg' } ] } ] } ];


I get too many file_rows in the first dir_row (should be 2), and there should only be 5 images per file_row. In the 2nd dir, it starts with an empty file_row..*shrug*

What am I doing wrong? Any help would be appreciated.

-Brian

Edit kudra, 2002-06-03 Added title given in ntc


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