foreach my $line (@file) { chop ($line); $line =~ s/ //g; my @values = split (/,/, $line); $thehash{$values[$field]} .= "$line\n"; } #### @keys = sort(keys %thehash); foreach (@keys) { print "$thehash{$_}"; }