#!/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"; } } }