Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

in function?

by Anonymous Monk
on Jul 07, 2000 at 23:56 UTC ( [id://21563]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Is there a way to quickly check if an element exist in an array? Common sense would say it would be
if ('blah' in @myarray) { do that }
which looks nice but unfortunatley doesn't work. Are there any build in functions which do that? I just wana check before I write one myself (2 lines :) - D e N u L L

Replies are listed 'Best First'.
Re: in function?
by ZZamboni (Curate) on Jul 08, 2000 at 00:17 UTC
    If it's a one-time test, using grep as suggested by plaid is the quickest way to go. If you are going to perform many tests, setting up a hash as suggested by ferrency will speed things up a lot in the long run.

    If you go the hash way, there is another, shorter way of initializing the array using a hash slice:

    @myhash{@myarray}=(1) x scalar(@myarray);
    Note that @myhash is being used, although %myhash is the one being initialized.

    --ZZamboni

Re: in function?
by ferrency (Deacon) on Jul 08, 2000 at 00:01 UTC
    This is best done using a hash instead of an array. But if you only have an array to begin with, it's easy to convert.
    (Warning, untested code below)

    # assume your array is still in myarray; set up a hash foreach (@myarray) { $myhash{$_} = 1; } # now, test for the existence of 'blah' if ($myhash{'blah'}) { do_something(); }
    If you use a hash instead of an array everywhere, you can also save the one-time overhead of setting up the hash in the first place. Using a hash makes the test closer to unit time than linear, which can speed things up significantly if you have a lot of array elements, or if you're doing a lot of "in"-like tests.

    Alan

      A more succinct (but semantically equivalent) version of:
      foreach (@myarray) { $myhash{$_} = 1; }
      is
      $myhash{$_}++ for @myarray;
      I once heard (somewhere) that doing
      if ($myhash{'blah'}) { do_something(); }
      (i.e. refering to a key in a hash) causes that key to come into existence, so the above expression may always evaluate as true. I haven't been able to prove this, but I think what you want is the exists function:
      if (exists($myhash{'blah'})) { # Do something here }
      I quite like using 'exists', it makes code far more readable.
Re: in function?
by le (Friar) on Jul 08, 2000 at 00:03 UTC
    The Cookbook offers an answer on how to find all elements in a array that match a certain criteria:
    @matching = grep { TEST $_ } @array;
Re: in function?
by plaid (Chaplain) on Jul 08, 2000 at 00:08 UTC
    While ferrency's answer is a better way to go about it overall, the most direct answer to your question would be
    if(grep {$_ eq 'blah'} @myarray) { # stuff }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://21563]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-25 12:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found