Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Getting Keys of Hash from Values

by davido (Cardinal)
on Nov 16, 2005 at 21:14 UTC ( #509200=note: print w/replies, xml ) Need Help??


in reply to Getting Keys of Hash from Values

If you're just doing it once, use ikegami's approach. If you're doing it many times (where the term 'many' is intentionally vague) you may benefit from maintaining two hashes; one forward-lookup and one reverse-lookup, if that makes sense.

Bear in mind, however, that in a hash, while the keys are guaranteed to be unique, there is no guarantee that the values are unique. So there's a possibility that 'yellow' as a value could occur several times, each under different unique keys. This must be dealt with somehow when doing reverse-lookups. One way is to just make sure you "don't do that" (don't create hash entries that have equal values). And it happens that maintaining a reverse-lookup hash is an excellent way of keeping track of two-way uniqueness. ...but this is just a tangent...


Dave

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2022-05-26 17:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (93 votes). Check out past polls.

    Notices?