in reply to Finding a hash key from the value (reverse lookup)

You could always use 'reverse':
#!/usr/bin/perl -w use strict; my %fruit = ( apple => 'green', orange => 'orange', banana => 'yellow' ); print ${{reverse %fruit}}{'yellow'};

Replies are listed 'Best First'.
Re: Re: Finding a hash key from the value (reverse lookup)
by PsychoSpunk (Hermit) on Jan 22, 2001 at 23:34 UTC
    You couldn't always use reverse.

    The question asked about non-unique values, thus you end up losing keys that are appropriate values. In this situation, you do not have a 1-1 function so reverse is very bad.

    ALL HAIL BRAK!!!

      Well, building a useful reverse index in cases where you don't have a 1:1 mapping isn't too bad (although not as elegant):

      (given %index)

      my %revindex; for my $revpair (map { [ $index{$_}, $_ ] } keys(%index)) { push @{$revindex{$revpair->[0]}}, $revpair->[1]; }

      There are probably simpler ways to do even that, however.