![]() |
||
![]() Watcom C/C++ Perl Examples Tutorials Imager Imager::Graph ImUgly Contact Us |
Inverting a hashBack to Perl ExamplesYou can think of this as an expansion on How do I look up a hash element by value? in perlfaq4.
Typically, if you want to invert a hash, you use
%inverted = reverse %original;
Here's a way to invert a hash, that preserves duplicate values:
sub invert_hash (\%) {
To use this:
use Data::Dumper; %x = ( a=>1, b=>2, c=>1, d=>1, e=>2, f=>3 ); print Dumper(\%x); %y = reverse %x; # the FAQ method, which shouldn't be used here %z = %{invert_hash(%x)}; print "reverse: keys which have value 1: ",join(',', $y{1}),"\n"; print "invert_hash: keys which have value 1: ", join(',', @{$z{1}}), "\n";This should produce a result like:
$VAR1 = { 'a' => 1, 'b' => 2, 'c' => 1, 'd' => 1, 'e' => 2, 'f' => 3 }; reverse: keys which have value 1: a invert_hash: keys which have value 1: a,c,dNote that the inverted hash produced with invert_hash()
has
|