Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Comparing against multiple values

by flocto (Pilgrim)
on Mar 18, 2002 at 21:38 UTC ( #152587=note: print w/replies, xml ) Need Help??

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 ;)

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://152587]
[LanX]: -i
LanX it's an imaginary list ...
LanX ... reflecting root problems
choroba uses real lists so he can easily insert as many items in between as needed
LanX questions the integerity of this approach. ..
LanX doesn't sound natural
[LanX]: welcome to a new episode of Big Perl Theory...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (16)
As of 2018-03-20 10:27 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (250 votes). Check out past polls.