http://www.perlmonks.org?node_id=152587


in reply to Comparing against multiple values

There are a few solutions that came to my mind:
my $code = "sub check { my \$val = shift; if ("; $code .= join (' or ', map { "\$val eq \"$_\""; } (@ceck_vals)); $code .= ") { return 1; } else { return 0; } }"; eval $code;
Propably quite performent if you have to check for these values very often and they never change (CGIs, for example..)
---
my %check = map { $_ => 1; } (@ceck_vals); if ($check{$val}) { do_foo (); }
Propably the better choice if @check_vals often changes..
---
if (grep { $val eq $_ } (@check_vals)) { do_foo (); }
I expect it to be kind of slow..
---
my $regex = join ('|', @check_vals); if ($val =~ m/$re/) { do_foo (); }
This is slow, but it can match parts of $val, in case this is needed..
---
There are more, but these were the ones I like the most :) Have fun ;)
Regards,
octo

P.S.: I didn't do ANY performance tests, so my guesses are likely to be wrong, please don't rely on this!