Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Get the Key corresponding to any given value

by rmalhotra (Initiate)
on Jun 28, 2002 at 09:11 UTC ( [id://177969]=perlquestion: print w/replies, xml ) Need Help??

rmalhotra has asked for the wisdom of the Perl Monks concerning the following question: (hashes)

Is there any way to get the key corresponding to a given value from a hash table

Originally posted as a Categorized Question.

  • Comment on Get the Key corresponding to any given value

Replies are listed 'Best First'.
Re: Get the Key corresponding to any given value
by jsprat (Curate) on Jun 28, 2002 at 18:18 UTC
    If the values are unique:

    my %rhash = reverse %hash; my $key = $rhash{$value};

    will give you the key for a given value.

    If you might have duplicate values, grep is your friend:

    my @keys = grep { $hash{$_} eq $value } keys %hash;

    If your values are numeric, use "==" instead of eq.
Re: Get the Key corresponding to any given value
by manav (Scribe) on Mar 01, 2005 at 18:07 UTC
Re: Get the Key corresponding to any given value
by codeacrobat (Chaplain) on Mar 26, 2006 at 10:51 UTC
    %h=(1 => 2, 3 => 2, 4 => 3); $val = 2; # use eq for strings, or whatever equals method for objects @keys = grep {$h{$_} == $val} keys %h; # (1,3)

    Originally posted as a Categorized Answer.

Re: Get the Key corresponding to any given value
by simmisam (Novice) on Jan 17, 2014 at 06:54 UTC
    my ($key) = grep {$value ~~ $hash{$_}} keys %hash; print "Key = $key\n";
      values are not necessarily unique so the left hand side should be a list.

      and using smart match doesn't make sense, better decide if == or eq are appropriate.

      simmisam did you notice that most of your recent answers in this category were rejected by QandAEditors b/c of poor quality?

      And (sadly) I have to say, I hope this one too!

      Maybe you should stop playing this game till you are more experienced?

      Cheers Rolf

      ( addicted to the Perl Programming Language)

        I'm actually OK with this one, but I would prefer that he would include an explanation of the value of his approach, i.e. describe the situations in which he thinks it could be useful.

        I reckon we are the only monastery ever to have a dungeon stuffed with 16,000 zombies.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2025-12-11 14:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (92 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.