in reply to What is the best way to find the key is exists or not in a hash ?

It would be really nice if you could tell us what your background is that you come to ask this. Im guessing that your experience is with VB and VBscript where collections are more or less crippled, would that be correct?

Anyway, to add some detail to the fully warranted sarcasm in this thread ill point out that an existance check on an associative array in perl performs in exactly the same time (possibly faster) as a fetch, and associative array fetches in perl are O(1). This is because the underlying algorithm, Hashing, has amortized O(1) properties in general which basically means its damn fast, in fact because of this and because the word is shorter in perl we call associative arrays "hashes". :-)

Unless the hash you are doing this check against is actually a tie into some kind of complex data structure that isnt using hashing you arent going to get any faster than the hash lookup that perl does. Oh, well maybe making your keys short will shave a nanosecond or two off the run time but i doubt it would be worth the loss of readability.

Reword your question with above knowledge integrated and you get the fairly easy to answer question

is there any mechanism faster than O(1) to do a lookup on a hashed data structure.

And the answer is NO.

---
$world=~s/war/peace/g

  • Comment on Re: What is the best way to find the key is exists or not in a hash ?