Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Removing duplicate values for a hash of arrays

by Kenosis (Priest)
on Nov 21, 2013 at 17:51 UTC ( #1063763=note: print w/ replies, xml ) Need Help??


in reply to Removing duplicate values for a hash of arrays

The best thing i could come up with was something that identified which keys have duplicate values but i don't know how to delete all but 1.

Can you share your code?

Edit - Here's one option:

use strict; use warnings; use Array::Compare; use Data::Dumper; my %hash = ( 'a' => [ '1', '2' ], 'b' => [ '2', '3' ], 'c' => [ '1', '2' ] ); my $comp = Array::Compare->new; for my $key1 ( keys %hash ) { for my $key2 ( keys %hash ) { next if $key1 eq $key2 or !$hash{$key1}; delete $hash{$key2} if $comp->compare( \@{ $hash{$key1} }, \@{ $hash{$key2} } ); } } print Dumper \%hash;

Output:

$VAR1 = { 'c' => [ '1', '2' ], 'b' => [ '2', '3' ] };

The or !$hash{$key1} notation avoids autovivification in cases where the key no longer exists.


Comment on Re: Removing duplicate values for a hash of arrays
Select or Download Code
Re^2: Removing duplicate values for a hash of arrays
by perlvroom (Acolyte) on Nov 21, 2013 at 19:47 UTC
    Thank you. This is what i needed. To answer another question from below, they are different arrays that happen to contain the same content.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1063763]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2015-07-07 07:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (87 votes), past polls