Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Getting Keys of Hash from Values

by davido (Cardinal)
on Nov 16, 2005 at 21:14 UTC ( [id://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?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2024-04-20 04:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found