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

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://152587]
help
Chatterbox?
[usemodperl]: perl is not broken, perl is being broken by the web being broken by https, for the love of banking apps on free wifi at coffee shops or something...
[Veltro]: Wondering, would it be so hard to write a small application that you can access using http which forwards the request over web ussing ssl?
[Veltro]: are there not even tools for that?
[usemodperl]: the breakage is so fresh i guess not, so it's up to us! :-)
[usemodperl]: someone has yet to make that future famous free http to https proxy written in perl and supported by ads, paypal, patreon, etc to make millions of dollars a minute!
[Veltro]: Why would this be fresh, I recon this is stuff that happens all the time (not Perl per se)
[Corion]: usemodperl: I'm sure you could even code that on the command line.
[usemodperl]: definitely

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2018-06-24 17:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (126 votes). Check out past polls.

    Notices?