
FishMonger
Veteran
Nov 28, 2010, 1:26 PM
Post #8 of 14
(895 views)
|
|
Re: [new bie] build a hash of lists
[In reply to]
|
Can't Post
|
|
I'm not sure I understand what you want to achieve. Do you want a single list/array of all filenames without regard to the directory that they are in, or do you want separate lists/arrays of filenames for each directory? What I'm suggesting is to use a HoA (Hash of Arrays) where the hash keys are the full directory paths and their values are an array of filenames within that directory. Here's a working example which I ran on my Windows box.
#!/usr/bin/perl use strict; use warnings; use File::Find; use Data::Dumper; my %dirfile; find(\&wanted, 'D:/perl'); # Dump the data structure. print Dumper(\%dirfile), "\n\n"; # now lets output the filenames as is in your last post. for my $dir ( keys %dirfile ) { print join("\n", @{ $dirfile{$dir} }), "\n"; } sub wanted { return unless -f; push @{ $dirfile{$File::Find::dir} }, $_; } Which outputs:
D:\perl>test.pl $VAR1 = { 'D:/perl/Fishmonger' => [ 'Example.pm' ], 'D:/perl' => [ 'Coder.pm', 'MyConfig.pm', 'perl-1.pl', 'Person.pm', 'test.pl' ], 'D:/perl/users' => [ 'ronb.tun' ] }; Example.pm Coder.pm MyConfig.pm perl-1.pl Person.pm test.pl ronb.tun Is that what you're looking for? If not, then please give a more complete description of what you need.
|