Finding unique values in a hash (Perl)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding unique values in a hash (Perl)
# 1  
Old 01-18-2013
Finding unique values in a hash (Perl)

Hi,

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

Code:
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
# 2  
Old 01-18-2013
This User Gave Thanks to spacebar For This Post:
# 3  
Old 01-19-2013
One way:
Code:
#!/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}});
 }
}

This User Gave Thanks to elixir_sinari For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl giving unexpected md5 hash values

I am trying to speed up creating a line by line hash file from a huge file using Perl. Here is my current (working but too slow) Bash code: (while read line; do hash=$(echo -n $line | md5sum); echo ${hash:0:32}; done)And here is my Perl code: perl -MDigest::MD5 -le 'foreach $line ( <STDIN> )... (3 Replies)
Discussion started by: Michael Stora
3 Replies

2. Shell Programming and Scripting

Perl : Assigning multile hash values to a single array

I know that @food = %fruit; Works. But how do I assign %fruit and %veggies to @food ? (2 Replies)
Discussion started by: popeye
2 Replies

3. Shell Programming and Scripting

Perl: Printing null hash values as a " "?

I'm filling in a table of values for grades. I decided to go with reading into a hash from the files but I'm coming up with an error when printing a value that does not exist. I need to know if I can on-the-fly print a space (" ") or blank in place of the grade. Here's what the output should... (2 Replies)
Discussion started by: D2K
2 Replies

4. Shell Programming and Scripting

Compare values of hashes of hash for n number of hash in perl without sorting.

Hi, I have an hashes of hash, where hash is dynamic, it can be n number of hash. i need to compare data_count values of all . my %result ( $abc => { 'data_count' => '10', 'ID' => 'ABC122', } $def => { 'data_count' => '20', 'ID' => 'defASe', ... (1 Reply)
Discussion started by: asak
1 Replies

5. Shell Programming and Scripting

Sorting values of hash in ascending order using Perl

I want to sort values of a hash in ascending order. my %records; for my $value (sort values %records){print $value,"\n";} When I use the above code I get values in this order: 1,10,11,2,3,4,5,6,7,8,9. But, I need values in my output in this order: 1,2,3,4,5,6,7,8,9,10,11. Can Someone... (1 Reply)
Discussion started by: koneru_18
1 Replies

6. Shell Programming and Scripting

AWK, Perl or Shell? Unique strings and their maximum values from 3 column data file

I have a file containing data like so: 2012-01-02 GREEN 4 2012-01-02 GREEN 6 2012-01-02 GREEN 7 2012-01-02 BLUE 4 2012-01-02 BLUE 3 2012-01-02 GREEN 4 2012-01-02 RED 4 2012-01-02 RED 8 2012-01-02 GREEN 4 2012-01-02 YELLOW 5 2012-01-02 YELLOW 2 I can't always predict what the... (4 Replies)
Discussion started by: rich@ardz
4 Replies

7. Shell Programming and Scripting

perl hash - using a range as a hash key.

Hi, In Perl, is it possible to use a range of numbers with '..' as a key in a hash? Something in like: %hash = ( '768..1536' => '1G', '1537..2560' => '2G' ); That is, the range operation is evaluated, and all members of the range are... (3 Replies)
Discussion started by: dsw
3 Replies

8. Shell Programming and Scripting

Perl Data Structure - Non unique values

I have the perl data structure and what i need to do is find all values in @{$extractColumns{'2'}{'D'}} which are not there in @{$extractColumns{'2'}{'M'}} but seems like i need to put a flag somewhere and i messed up foreach my $order (keys %extractColumns) { foreach my $value... (2 Replies)
Discussion started by: dinjo_jo
2 Replies

9. Shell Programming and Scripting

Perl Hash:Can not keep hash data in the same order that it was inserted

Can Someone explain me why even using Tie::IxHash I can not get the output data in the same order that it was inserted? See code below. #!/usr/bin/perl use warnings; use Tie::IxHash; use strict; tie (my %programs, "Tie::IxHash"); while (my $line = <DATA>) { chomp $line; my(... (1 Reply)
Discussion started by: jgfcoimbra
1 Replies

10. Shell Programming and Scripting

perl-extract data from hash values

Hello, I have parsed an xml file using perl to get the hash values and the output looks like this $VAR1 = { 'RT' => { 'List' => { 'String' => ... (1 Reply)
Discussion started by: userscript
1 Replies
Login or Register to Ask a Question