my %maxMatch; for my $key (keys %$match) { @{$match->{$key}} = uniq @{$match->{$key}}; my $seqSet = join ",", @{$match->{$key}}; unless ($maxMatch{$seqSet}) { $maxMatch{$seqSet} = $key; next; } my $old = true { /[^\.]/ } split //, $maxMatch{$seqSet}; my $new = true { /[^\.]/ } split //, $key; $maxMatch{$seqSet} = $key if $new > $old; } for my $key (keys %maxMatch) { print $maxMatch{$key}, " occurs in $key \n"; }