Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Finding a hash key from the value (reverse lookup)

by kilinrax (Deacon)
on Jan 22, 2001 at 18:36 UTC ( [id://53493]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://53493]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-20 02:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found