Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

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

by Anonymous Monk
on May 23, 2000 at 19:24 UTC ( [id://14375]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Something of this nature:
if ((a value from %hash) eq "Superman"){ do something..; }

Replies are listed 'Best First'.
Re: How do I check to see if a value in a hash is equal to something?
by ZZamboni (Curate) on May 23, 2000 at 19:44 UTC
    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

      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
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://14375]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (10)
As of 2024-04-18 16:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found