If it's just a one-time check, or if the array changes frequently, it's probably not all that efficient to suck the whole thing into a hash just to do three exists tests. Converting the big set to a hash is an O(n) operation, but a computationally more expensive one than the O(n) operation of grepping for the thee elements.

my %find; @find{ qw( this that other ) } = (); my $found = grep { exists $find{$_} && ( delete $find{$_} || 1 ); } @b +ig_list; print "Success!\n" if $found == 3;

This method uses a small three-element hash as the lookup list. Each time an entry is found, it is deleted from the lookup hash so that you assure that all three items are found (as opposed to the same item three times).

If the big array changes infrequently, and you will be doing more than a few lookups on it, then it makes sense to convert the big array to a hash and invert the search:

my %big_set; @big_set{ @big_array } = (); my @find = qw( this that other ); my $found = grep{ exists $big_set{$_} } @find; print "Success!\n" if $found == 3;

Dave


In reply to Re: Check Array Elements by davido
in thread Check Array Elements by mmittiga17

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.