CrackBaby has asked for the wisdom of the Perl Monks concerning the following question:

Good morning Monks. I hope some more experienced Perl programmers can help me with a small task. I need to find and report duplicate arrays in a hash of arrays. I do not care about unique arrays, I need to know which arrays exist in the hash 2 or more times. Something like this:
my %hoa = ( array1 => [ 1,2,3 ], array2 => [ 7,8,9 ], array3 => [ 1,2,3 ], array4 => [ 7,8,9 ], array5 => [ 9,9,9 ], array6 => [ 1,2,3 ], );
I need to know which arrays exist 2 or more times - in this example I need to know that array1, array3 and array6 match - and also array2 and array4 match. Thank you for your help.

Replies are listed 'Best First'.
Re: Find matching arrays in hash of arrays
by moritz (Cardinal) on Nov 04, 2008 at 17:30 UTC
    Something along these lines:
    use strict; use warnings; my %hoa = ( array1 => [ 1,2,3 ], array2 => [ 7,8,9 ], array3 => [ 1,2,3 ], array4 => [ 7,8,9 ], array5 => [ 9,9,9 ], array6 => [ 1,2,3 ], ); my %counts; while (my ($k, $v) = each %hoa) { push @{$counts{join '|', @$v}}, $k; } print "Duplicates:\n"; for (keys %counts) { next if @{$counts{$_}} < 2; print join ', ', @{$counts{$_}}; print "\n"; } __END__ Duplicates: array1, array3, array6 array4, array2
      Yes, that work perfectly, thank you.
Re: Find matching arrays in hash of arrays
by GrandFather (Saint) on Nov 04, 2008 at 19:52 UTC

    How big are the arrays? If they are small then you could:

    use warnings; use strict; my %hoa = ( array1 => [ 1,2,3 ], array2 => [ 7,8,9 ], array3 => [ 1,2,3 ], array4 => [ 7,8,9 ], array5 => [ 9,9,9 ], array6 => [ 1,2,3 ], ); my %dups; push @{$dups{join '~', @{$hoa{$_}}}}, $_ for sort keys %hoa; print map {join (" ", @{$dups{$_}}), "\n"} grep @{$dups{$_}} > 1, keys + %dups;

    Prints:

    array1 array3 array6 array2 array4

    Perl reduces RSI - it saves typing