Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Answer: How to find out if X is an element in an array?

by little_mistress (Monk)
on Apr 01, 2000 at 04:30 UTC ( #6637=categorized answer: print w/replies, xml ) Need Help??

Q&A > arrays > How to find out if X is an element in an array? - Answer contributed by little_mistress

You could do it like this:
my $element = "catchme"; my @array = ( "catchme", "if", "you", "can" ); if ( @found = grep { $_ eq $element } @array ) { my $found = join ",", @found; print "Here's what we found: $found\n"; } else { print "Sorry, \"$element\" not found in \@array\n"; }
I know that's a little longer than other answers but, I think this gives a little more detail and is easier to see how it works.

Replies are listed 'Best First'.
Re: Answer: How to find out if X is an element in an array?
by IlyaM (Parson) on Dec 08, 2001 at 02:56 UTC
    In most cases my should be used for declaration of variables instead of local. In most cases lexically scoped variables are more appropriative. See perlsub for details.

    --
    Ilya Martynov (http://martynov.org/)

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2020-10-19 21:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (207 votes). Check out past polls.

    Notices?