http://qs1969.pair.com?node_id=509200


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