Perl-Sensitive Sunglasses PerlMonks

### check if a value is in a given set

by Ratazong (Monsignor)
 on Jul 24, 2012 at 14:26 UTC Need Help??
Ratazong has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I want to check if a value is inside a set of predefined values. Which is the best way to do so? I came to the following solution, but somehow I have the feeling there must be a more elegant way...

my \$var = "apple"; if (exists( { banana => 1, plum => 1, apple => 1, strawberry => 1, pea + => 1,}->{\$var} )) { print "yes!\n"; } else { print "no.\n"; }
Rata (failing to google it himself)

Replies are listed 'Best First'.
Re: check if a value is in a given set
by choroba (Bishop) on Jul 24, 2012 at 15:34 UTC
I tend to use hashes if the set is larger, but I stay with grep if not and the test is not repeated much:
#!/usr/bin/perl use warnings; use strict; my @values = qw/banana plum apple strawberry pea/; my \$var = 'apple'; my %hash; undef @hash{@values}; print exists \$hash{\$var} ? "yes\n" : "no\n"; print +(grep \$_ eq \$var, @values) ? "yes\n" : "no\n";
Re: check if a value is in a given set
by Anonymous Monk on Jul 24, 2012 at 15:33 UTC

Smart-matching seems to work OK.

use 5.010; my @set = qw/banana plum apple strawberry pear/; my \$var = "apple"; if (\$var ~~ @set) { say "yes"; }

There are also hashes which might be a tad faster if your set is large.

my %set = map { \$_ => 1 } qw/banana plum apple strawberry pear/; if (exists \$set{\$var}) { say "yes"; }
Re: check if a value is in a given set
by hippo (Abbot) on Jul 24, 2012 at 16:21 UTC
Re: check if a value is in a given set
by brx (Pilgrim) on Jul 24, 2012 at 15:40 UTC

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".
Re: check if a value is in a given set
by Marshall (Abbot) on Jul 25, 2012 at 03:48 UTC
I want to check if a value is inside a set of predefined values.

I would use grep.

#!/usr/bin/perl -w use strict; my @fruits = qw(banana plum apple strawberry pea); if ( grep {/apple/}@fruits ) #perhaps /^apple\$/ { print "apple found!\n"; } else { print "apple not fouund\n"; } __END__ apple found!
There are other ways see: List::Util
Re: check if a value is in a given set
by 2teez (Vicar) on Jul 24, 2012 at 15:51 UTC

I will probably write this using List::Util like so:

#!/usr/bin/perl use warnings; use strict; use List::Util qw(first); my \$var = 'apple'; my \$h_ref = { banana => 1, plum => 1, apple => 1, strawberry => 1, pea => 1, }; my \$foo = first { \$_ eq \$var } keys %{\$h_ref}; defined \$foo ? print "yes" : print "No";
If your \$var exist, it prints "Yes", if not you get a "No"

You have a set of hash keys, and enumerate through them to find whether the one you seek exists?

Create A New User
Node Status?
node history
Node Type: perlquestion [id://983426]
Front-paged by Corion
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?