Finding unique values in a hash (Perl)

Hi,

I have a hash with unique keys associated with some data.

my %FINALcontigs = (
        'mira_rep_c765:119reads**', 'ctctactggaagactgac',
        'mira_rep_c7454:54reads**', 'atggatactgcgctgttgctaactactgga',
        'mira_rep_c6803:12reads**', 'atcgactggatgcagggttgtggtttcta',
        'mira_rep_c1661:6reads**', 'ctctactggaagactgac',
    );

Notice that 'mira_rep_c765:119reads**' and 'mira_rep_c1661:6reads**' have the same value. I need to find which keys have matching values. What is the easiest way to do this?

Thanks

Take a look at the info on these links:
Perl - Printing duplicates in a hash
How do I find and count duplicate values in a perl hash - Stack Overflow
hth

1 Like

One way:

#!/usr/bin/perl
use strict;
use warnings;

my %FINALcontigs = (
        'mira_rep_c765:119reads**', 'ctctactggaagactgac',
        'mira_rep_c7454:54reads**', 'atggatactgcgctgttgctaactactgga',
        'mira_rep_c6803:12reads**', 'atcgactggatgcagggttgtggtttcta',
        'mira_rep_c1661:6reads**', 'ctctactggaagactgac',
    );

my %seen_keys;

for my $ha_key (keys %FINALcontigs) {
 push @{$seen_keys{$FINALcontigs{$ha_key}}}, $ha_key
}

for my $ha_key (keys %seen_keys) {
 if(@{$seen_keys{$ha_key}} > 1) {
  print "\nDuplicate keys for value $ha_key:\n";
  print "$_\n" for (@{$seen_keys{$ha_key}});
 }
}
1 Like