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


In reply to Re: What is the best way to find the key is exists or not in a hash ? by demerphq
in thread What is the best way to find the key is exists or not in a hash ? by swaroop

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.