Being as you're fairly new here, I'm not going to rag on you too much, but you need to understand something -- this site welcomes questions about Perl, but it's important to be able to explain
Anyway, here's a self-contained script that works and (I think) does what you want it to:
When this is run, it produces#!/usr/bin/perl -w use strict; my @data = ( "3343df3ffdkj34j3k34j3k testfile1", "389k34d46hj3k493843kjj testfile2", "lj3l4o342u423see3u43u4 testfile3", # Copied the first line to show that duplicates work. "3343df3ffdkj34j3k34j3k testfile4", ); { my %result; # Loop through the array of lines. foreach my $line (@data) { # Split the array element into hash value and filename. my ( $hashValue, $filename ) = split( /\s/, $line ); # Store the filename, indexed by hash value, into an array. This # allows us to store multiple files with the same hash value. push( @{ $result{$hashValue} }, $filename ); } # 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"; } } }
I'm not sure if that's what you were looking for, but that's my solution for my best guess at what you're looking for.tab@foobar:~dev$ perl -w 736240.pl 3343df3ffdkj34j3k34j3k -> testfile1 3343df3ffdkj34j3k34j3k -> testfile4 389k34d46hj3k493843kjj -> testfile2 lj3l4o342u423see3u43u4 -> testfile3
Now, please note the copious (and probably unnecessary) comments and the logical variable names in my script. Your original code had an array called 'hashes'. That's only confusing initially, but it's not a great name, I would have used digests or something like that. But the biggest problem with your code is that there were no comments.
Once more, with feeling:
In reply to Re^3: Sorting an array of hashes and filenames
by talexb
in thread Sorting an array of hashes and filenames
by learningperl01
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |