Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Oracle IN like operator in Perl

by paragkalra (Scribe)
on Dec 28, 2009 at 10:31 UTC ( [id://814588]=perlquestion: print w/replies, xml ) Need Help??

paragkalra has asked for the wisdom of the Perl Monks concerning the following question:

Hello All,

Wanted to know if we have a IN like operator in Perl just the way we have in Oracle.

My requirement is to check if particular value is one the values of a list.

I know there are many ways to do so but I am looking for a one liner operator.

Replies are listed 'Best First'.
Re: Oracle IN like operator in Perl
by Old_Gray_Bear (Bishop) on Dec 28, 2009 at 10:38 UTC
    Take a look at List::MoreUtils, specifically any().

    ----
    I Go Back to Sleep, Now.

    OGB

Re: Oracle IN like operator in Perl
by JavaFan (Canon) on Dec 28, 2009 at 12:57 UTC
    if ($val ~~ ['val1', 'val2', 'val3', 'val4']) {...}
Re: Oracle IN like operator in Perl
by amir_e_a (Hermit) on Dec 28, 2009 at 19:07 UTC

    ~~, the Smatch Match Operator introduced in Perl 5.10 and already mentioned by JavaFan is probably the best thing for you. See Smart matching in detail in the Perl documentation for more details.

    It only works in Perl 5.10 and later. If you are using an earlier version, you should consider upgrading.

    If you can't upgrade, grep and the functions in List::Util and List::MoreUtil may do what you need. Note that grep has a performance price, as it always goes over the entire list, but on small lists it's not significant.

Re: Oracle IN like operator in Perl
by Anonymous Monk on Dec 28, 2009 at 15:43 UTC

    Something like this might work?

    grep /\bwhatever\b/,@array;

Log In?
Username:
Password:

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

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

    No recent polls found