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

One thing I really like in Perl is the number of different operators... I find it's nicer to express things idiomatically instead of using lots of if-else branching. With that in mind I'm wondering if there's a way to test whether a variable has one of a set of possible values... something similar to SQL concept of "WHERE id IN (13,16,18)" So instead of writing:
if ($myfruit eq "bananas" || $myfruit eq "apples" || $myfruit eq "oran +ges"){ print "I like $myfruit!"; } else { print "I don't like $myfruit!"; }
I could write
if ($myfruit in ("bananas","apples","oranges")){ print "I like $myfruit!"; } else { print "I don't like $myfruit!"; }
Any ideas? I know that typically hash keys are used for set comparisons, but it's less elegant because I'd have to create the hash just for the comparison.

Replies are listed 'Best First'.
Re: IN-style operator for set of possible values
by LanX (Saint) on Mar 16, 2012 at 00:42 UTC
    see smart-match-operator

    DB<102> "bananas" ~~ ["bananas","apples","oranges"] => 1 DB<103> "bananas" ~~ ["apples","oranges"] => ""

    Cheers Rolf

Re: IN-style operator for set of possible values
by toolic (Bishop) on Mar 16, 2012 at 00:46 UTC
    grep
    use warnings; use strict; my $myfruit = 'apples'; if (grep { $myfruit eq $_ } ("bananas","apples","oranges")){ print "I like $myfruit!"; } else { print "I don't like $myfruit!"; }

    See also:

Re: IN-style operator for set of possible values
by BrowserUk (Patriarch) on Mar 16, 2012 at 01:00 UTC

    This is almost exactly what you've asked for, but less typing:

    if( $myfruit =~ m[bananas|apples|oranges] ){ print "I like $myfruit!"; } else { print "I don't like $myfruit!"; }

    To be absolutely certain, you could use: m[^(?:bananas|apples|oranges)$]


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

Re: IN-style operator for set of possible values
by AnomalousMonk (Archbishop) on Mar 16, 2012 at 01:00 UTC
    ... typically [hashes] are used..., but it's less elegant because I'd have to create the hash just for the comparison.

    In all the pseudo or real solutions
        if ($myfruit in ("bananas","apples","oranges")){ ... }
        if ("bananas" ~~ ["bananas","apples","oranges"]) { ... }
        if (grep { $myfruit eq $_ } qw(bananas apples oranges)){ ... }
    a set must be created just for the comparison. (However, the match, smart-match and grep solutions allow matching for something other than equality.)

Re: IN-style operator for set of possible values
by kejohm (Hermit) on Mar 16, 2012 at 01:47 UTC

    You can always rely on the power of quantum computing:

    use 5.012; use Quantum::Superpositions; my $myfruit = 'apples'; if ( $myfruit eq any( qw(bananas apples oranges) ) ) { print "I like $myfruit!"; } else { print "I don't like $myfruit!"; }
      You can always rely on the power of quantum computing:

      That's like choosing the Large Hadron Collider to pop your corn, just because it has a long and impressive name. It might get there eventually, but it takes an awful long time.

      Below, Quantum::Superpositions::any() takes 15 whole seconds to do what List::MoreUtils::any() does in 0.1 second. (plain ol'grep is quicker still!)

      Don't be overly impressed with the spiel, Q::S is just a bunch of (s-l-o-w) loops underneath:

      @a=1..1000; cmpthese -1,{ a=>q[ my$n=0; for my $i (1..1000) { Quantum::Superpositions::any( +@a ) eq $i and ++$n } ], b=>q[ my$n=0; for my $i (1..1000) { List::MoreUtils::any{ $i eq $_ + } @a and ++$n } ], };; (warning: too few iterations for a reliable count) s/iter a b a 15.4 -- -99% b 0.103 14834% --

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      The start of some sanity?

        And this is probably one of several reasons that Damian Conway places Quantum::Superpositions in the "Damian modules you shouldn't use in production because their purpose is to explore and prototype future core language features" category in his Categorized Damian Modules list.

        The performance sucks, but, at least according to TheDamian, it was never intended to be used in production.


        Dave

        Yeah, you're right. It was more in the spirit of TIMTOWTDI.

        That's like choosing the Large Hadron Collider to pop your corn...

        mmm, quantum popcorn :)