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.
|
|---|
| 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 |