Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

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;


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

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

Replies are listed 'Best First'.
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?

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (11)
As of 2016-10-28 15:52 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (386 votes). Check out past polls.