Suppose you have %hash, and you want to show all the keys that share the same given value. %revhash = reverse %hash won't work because of the value collision. Solution: use arrayrefs for values during the inversion!
my %revhash; while (my ($key, $value) = each %hash) { push @{$revhash{$value}}, $key; } for (sort keys %revhash) { print "$_ => @{$revhash{$_}}\n"; }
(Revised per mdillon's note, but in a different way for speed)

Replies are listed 'Best First'.
RE: Inverting a hash to get all keys for this value
by mdillon (Priest) on Oct 09, 2000 at 21:11 UTC
    i think you meant this:
    my %revhash; push @{$revhash{$hash{$_}}}, $_ for keys %hash; for (sort keys %revhash) { print "$_ => @{$revhash{$_}}\n"; }