Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: check if a value is in a given set

by brx (Pilgrim)
on Jul 24, 2012 at 15:40 UTC ( [id://983441]=note: print w/replies, xml ) Need Help??


in reply to check if a value is in a given set

Elegant? Hard to say...

Smartmatch:

perl -e 'my @t=qw/banana plum apple strawberry pea/;my $fruit="apple"; +print (($fruit~~\@t)?"yes":"no");' yes

Undef hash:

perl -e 'my @t=qw/banana plum apple strawberry pea/;my $fruit="apple"; +my %in;undef @in{@t};print ((exists $in{$fruit})?"yes":"no");' yes

for,next,last:

perl -e 'my @t=qw/banana plum apple strawberry pea/;my $fruit="apple"; +for my $f (@t) {next if $fruit ne $f;print "yes";last}' yes

grep?

#no, not efficient

English is not my mother tongue.
Les tongues de ma mère sont "made in France".

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-26 07:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found