in reply to Re: Nested loops in HTML::Template
in thread Nested loops in HTML::Template

Sorry about the previous anonymous posts -- i couldn't get my password at the machine I was using.

Anyway, my next (logical) follow-up question is this: Given an array of directories (@dirs) and the ability to get all of the filenames in each of those dirs into an array (@files), what's the best way to build the structure you describe in your post?

would you do something like:
foreach (@dirs) { # get filenames into @files # push dir name...? foreach (@files) { # do some sort of magic with push();, # taking into account when you get to the # fifth filename...? } }
I was able to figure out how to use push() to loop through the dir names, but adding the sub-structures is confusing :)

-Brian

Replies are listed 'Best First'.
Re: Building the structure to hold said beast.
by vladb (Vicar) on May 30, 2002 at 14:17 UTC
    LTjake, give this a shot:
    #!/usr/bin/perl use strict; use File::Find; use Data::Dumper; my $dir_list; # count of { file_row => [...] } entries inside # dir_row array. (err.. simply dir_row array's size). my $dir_row_count = 0; my $file_count = 0; # number of files in a row.. my $file_split_count = 5; # previous directory my $prev_dir; find(\&wanted, 'data'); print Dumper($dir_list); ### SUBS sub wanted { $DB::single = 1; unless ($prev_dir eq $File::Find::dir) { $prev_dir = $File::Find::dir; # got a new directory push @$dir_list, { dir => $File::Find::dir, dir_row => [] }; } if (-f $_) { unless ($file_count % $file_split_count) { # add another file row if maximum number of files # allowed in a row has been exceeded. push @{$dir_list->[scalar @$dir_list - 1]{dir_row}}, { file_row +=> [] }; } # add file to the file row... push @{$dir_list->[scalar @$dir_list - 1]{dir_row}[$dir_row_count] +{file_row}}, { filename => $File::Find::name }; $dir_row_count++; $file_count++; } }
    Unfortunately, this is an untested piece as I have no 'good' means to thoroughly test it on the box I"m on now (my MS-DOS command prompt doesn't work for some freakn reason.. ). I'll do the testing on my Unix box at work (during coffee time, yeah! ;-)

    Note: I post it here in hopes someone picks it up from where I left or at leats finds it useful in his own rendition of a solution. (although, having this reply nested so deep, I'm not sure if many monks out there will notice it ;)

    _____________________
    $"=q;grep;;$,=q"grep";for(`find . -name ".saves*~"`){s;$/;;;/(.*-(\d+) +-.*)$/; $_=["ps -e -o pid | "," $2 | "," -v "," "];`@$_`?{print"+ $1"}:{print" +- $1"}&&`rm $1`; print$\;}
Re: Building the structure to hold said beast.
by talexb (Chancellor) on May 30, 2002 at 13:57 UTC
    It's not exactly clear what you're after. Can you add links to your original Anonymous posts? That way we'll be able to respond to your posts more efficiently.

    --t. alex

    "Nyahhh (munch, munch) What's up, Doc?" --Bugs Bunny