I think you may have misunderstood how File::Find works -- when you call the find method, it's going to call your (mis-named) search function for each file that it finds.
I would rename that function to storeFileHashValue to better reflect what it's actually doing. Then I'd move the declaration of %result into the file scope, and dump out the results of your file scanning after you call find.
So your code would look something like this:
I haven't tried this. Last night I was at home. Right now I'm on the job.#!/usr/bin/perl use strict; use File::Find; use File::stat; use Digest::MD5; my %result; find(\&storeHashvalue, "/home/test/"); # Dump out the result hash, sorting by the hash values. foreach my $key ( sort keys %result ) { # Dump out the array of filenames indexed by this hash value. We # could have sorted this list too if we wanted. foreach my $filename ( @{ $result{$key} } ) { print "$key -> $filename\n"; } } sub storeHashvalue { # Make sure we only look at files and end in src extn. if ( -f && /\.src$/i ) { foreach my $files ( $File::Find::name ) { open(FH, $files) or die "Can't open '$files': $!"; binmode(FH); # Get/Create MD5 for the file my $hashValue = Digest::MD5->new->addfile(*FH)->hexdigest; # Store the filename, indexed by hash value, into an arra +y. This # allows us to store multiple files with the same hash va +lue. push( @{ $result{$hashValue} }, $files ); } } }
In reply to Re^5: Sorting an array of hashes and filenames
by talexb
in thread Sorting an array of hashes and filenames
by learningperl01
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |