Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Answer: How do I check to see if a value in a hash is equal to something?

( #14390=categorized answer: print w/ replies, xml ) Need Help??

Q&A > hashes > How do I check to see if a value in a hash is equal to something? contributed by ZZamboni

One way would be by inverting the hash and looking things up by value, as described in perlman:perlfaq4 (see How do I look up a hash element by value?).

Another way, of you are only interested in knowing if the value is in the hash, without knowing the key, could be:

$is_in_hash=grep /^Superman$/,values(%hash);
This is not particularly space efficient if the hash is large. See also the second solution suggested in the FAQ mentioned above.

--ZZamboni

Comment on Answer: How do I check to see if a value in a hash is equal to something?
Download Code
Replies are listed 'Best First'.
RE: Answer: How do I check to see if a value in a hash is equal to something?
by merlyn (Sage) on May 23, 2000 at 19:47 UTC
    It has been said:
    Another way, of you are only interested in knowing if the value is in the hash, without knowing the key, could be:
    $is_in_hash=grep /^Superman$/,values(%hash);
    This is not particularly space efficient if the hash is large. See also the second solution suggested in the FAQ mentioned above.
    Rather than using a regular expression, just use an ordinary expression:
    $is_in_hash = 0 < grep $_ eq "Superman", values %hash;

    -- Randal L. Schwartz, Perl hacker

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (17)
As of 2015-07-30 13:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (271 votes), past polls