in reply to data structure advice please
my %dupes = (); # name->count my $dref = \%dupes;
You can, but there is no need. And in this case you're not passing the hash around, so I've left it as a hash, rather than a hash-ref.
Where you DO need a ref is where you store the filedata in %fileinfo. You want an array containing all the files with name X, and you store an array as a hash value by using an array ref.
@file_info = qw(data1 data2 data3); %files = ( X => \@file_info )
If you want to add something to the array, then you need to use push, which means you need an array, not an array ref, so you need to dereference it:
$file_info_ref = $files{X}; push @$file_info_ref,'data5'; or push @{$files{X}}, 'data5'
So:
my %fileinfo; my %dupes; # for each file, recursing through directory tree foreach { # here $_ is each file name # if filename seen already if( exists $fileinfo{$_} ) { # then record in %dupes $dupes{$_} = 1; } @filedata = ( $fpath, $fsize, $fdate ); # but using real data # if fileinfo doesn't yet have an entry for this $_, # then assign an empty array ref # saved_info is now an array ref which is # also stored in $fileinfo{$_} $saved_info = $fileinfo{$_} ||= []; push @$saved_info,\@filedata; # No need to re-store it in the hash, because $fileinfo{$_} # and $saved_info both point to the same array }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: data structure advice please
by Arunbear (Prior) on Nov 25, 2006 at 19:50 UTC | |
by clinton (Priest) on Nov 25, 2006 at 19:59 UTC | |
Re^2: data structure advice please
by anadem (Scribe) on Nov 25, 2006 at 21:41 UTC |