note
Eily
<p>With a regular hash you just have to do:
<c>%reverseHash = reverse %hash;
%hash = reverse %reverseHash;</c>
to eliminate duplicate values. Which of course doesn't work in your case, because the arrays in @hash{'a', 'c'} are no the same, though they contain the same values, and so have different references. Well, you could always turn those arrays into something identical. If you have someFunc(PARAM) that returns the same thing if and only if the values in the data structure PARAM are identical, you can do:
<c>my %reverseHash;
while(($key, $value)=each %hash)
{
$processedValue = someFunc($value);
$reverseHash{$processedValue} = $key;
}
my @keys = values %reverseHash;
my %newHash;
@newHash{@keys} = @hash{@keys};</c>
</p>
<p>The thing is, there is an easy solution for someFunc : [mod://Data::Dumper] (and this time I'll use [doc://map])
<c>use Data::Dumper;
my %hash = (
a => [1, 2],
b => [2, 3],
c => [1, 2]
);
my %reverseHash = map { Dumper($hash{$_}) => $_ } keys %hash;
%hash = map { $_ => $hash{$_} } values %reverseHash;
print Dumper \%hash;
</c></p>
<p><b>Edit:</b> this works well for nested arrays, even [bless]ed ones. But with hashes, you probably have to activate [mod://Data::Dumper]'s key sorting (It depends of the version of Perl you are using).</p>
1063761
1063761