Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find and count duplicate values in a perl hash

Tags:

perl

I need to find the duplicate values in a perl hash and then output the key/value pair and associated dup count when that count is > 1.

(I could leave a code sample of what I've attempted but that would just result in mass confusion and possibly uncontrolled laughter, and I'm really hoping to make it through life with some semblance of self esteem.)

Hash key/value would look like the following:

%hash = qw('FHDJ-124H' => 'hostname1', 'HJDHUR-87878' => 'hostname2', 'HGHDJH-874673' => 'hostname1');

My desired output would be:

2 duplicates found for hostname1
    FHDJ-124H
    HGHDJH-874673

Using perl 5.6 on Solaris 10. Tightly controlled production environment where upgrading or loading perl mods is not allowed. (A change request for moving to 5.8 is about 6 months out).

Many thanks!

like image 681
Shawn Anderson Avatar asked Dec 09 '22 08:12

Shawn Anderson


1 Answers

You need to iterate through the hash keys in your first hash (key/value) and accumulate the count of each item you find in another hash (value/count).

If you want to display the keys together with duplicated values, your second hash cannot be as simple as that, since for each duplicated value you will have a collection of keys (all of them having the same value). In this case, simply accumulate the key in an array, then count its elements. I.e., your second hash would be something like (value/[key1,key2,key3...])

my %hash = ( key1 => "one", key2 => "two", key3 => "one", key4 => "two", key5 => "one" );
my %counts = ();
foreach my $key (sort keys %hash) {
    my $value = $hash{$key}; 
    if (not exists $counts{$value}) {
        $counts{$value} = [];
    }
    push $counts{$value}, $key;
};

Then iterate over $counts to output what you need when the count of elements in $counts{$value} > 1

like image 57
sergio Avatar answered Jan 08 '23 09:01

sergio