Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Answer: 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 ( #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? - Answer 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?
[Corion]: Discipulus++ # being well is like being earnest
[marto]: hopefully next year I'll be in a position to go to Perly things in Europe :)
[Corion]: Maybe I'll use the Real Jekyll a bit to get a feel for how it actually works, and to get annoyed by it ;)
[Corion]: marto: That would be cool :) There is no kids care at Perly events unfortunately, so it's not entirely family friendly
[Corion]: I think I should define a set of (say) five projects through which I rotate from time to time, just to prevent myself from being bored/annoyed by the problems they get stuck in ;)
[marto]: Corion, not bringing the kids would make it ideal :P
[marto]: a suggestion, have one project in the list that isn't programming/perl related
[Corion]: marto: Yeah, things are more parent-friendly the way they are ;))
[marto]: some hardware/makey thing
[Corion]: marto: Ah, I'm currently drifting to that again, over"painting" (well with Inkscape) one of my photographs and thinking about vectorizing some other images ...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2017-07-27 07:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I came, I saw, I ...
























    Results (404 votes). Check out past polls.