Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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

by little_mistress (Monk)
on Apr 01, 2000 at 04:30 UTC ( [id://6637]=note: print w/replies, xml ) Need Help??


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

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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://6637]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (2)
As of 2024-04-20 06:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found