in reply to data structure advice please

You're getting a bit mixed up with all the sigils (%@$). First, there's no need to do this:

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
    you could shorten
    $saved_info = $fileinfo{$_} ||= []; push @$saved_info,\@filedata;
    to
    push @{ $fileinfo{$_} }, \@filedata;
    beacause the arrayref will be autovivified if it didn't already exist.
      True. I thought it may have complained that 'undef' (the initial value of $fileinfo{$_}) was not an array ref, but after testing it, I see it doesn't.
Re^2: data structure advice please
by anadem (Scribe) on Nov 25, 2006 at 21:41 UTC
    thanks, especially for the explanations of 'why' - useful in reducing my ignorance! (I should have said I'd used a reference so it could be passed around to other yet-to-be-done subroutines.)