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

In your case, we have a fragment of Perl that doesn't compile. This isn't helpful to us, so our answers may not be very useful to you. A complete working example of what isn't working is way more use to us.

Anyway, here's a self-contained script that works and (I think) does what you want it to:

#!/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"; } } }
When this is run, it produces
tab@foobar:~dev$ perl -w 736240.pl 3343df3ffdkj34j3k34j3k -> testfile1 3343df3ffdkj34j3k34j3k -> testfile4 389k34d46hj3k493843kjj -> testfile2 lj3l4o342u423see3u43u4 -> testfile3
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.

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:

Alex / talexb / Toronto

"Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds


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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.