Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

*IN* Operator for the Perl wish list

by hangon (Deacon)
on Mar 30, 2007 at 12:31 UTC ( #607459=perlmeditation: print w/replies, xml ) Need Help??

I had been using SQL long before discovering Perl and became quite fond of the SQL *IN* operator. Not having found the equivalent in Perl, I usually construct a hash with my values as the keys. Unless something similar has already been done (and I have not discovered it yet), would this be something worth petitioning the Perl gods for?

# What I want to do if ($value == 1 or $value == 5 or $value == 21 or $value == 99){... # How I often do it: my %list = (1 => 1, 5 => 1, 21 => 1, 99 => 1); if ($list{$value}){... # Wouldn't something like this be better? if ($value IN (1, 5, 21, 99)){...

UPDATE: Thanks everyone. It looks like Quantum::Superpositions does what I had in mind (didn't find it initially searching CPAN). Since I'm still trying to fill the holes in my *swiss cheese* knowledge of Perl 5, I haven't looked into Perl 6 much.

Replies are listed 'Best First'.
Re: *IN* Operator for the Perl wish list
by davorg (Chancellor) on Mar 30, 2007 at 12:46 UTC

      If there was a poll for the least sensibly named CPAN module Quantum::Superpositions is sure to get my vote. The name belongs to the Acme:: namespace.

Re: *IN* Operator for the Perl wish list
by educated_foo (Vicar) on Mar 30, 2007 at 14:47 UTC
    You can also just do
    grep $_ == $value, 1,5,21,99;
    which isn't much more typing.
      Rather than "grep", I would use List::Util's "first" function, because that short-circuits on the first match, rather than always trying all of the elements.


        I haven't benchmarked it, but I suspect first has significantly more overhead, so might only be of benefit for long lists. I'd be interested in seeing a benchmark.

        Update: yes, I mean even the XS version

Re: *IN* Operator for the Perl wish list
by lidden (Curate) on Mar 30, 2007 at 12:49 UTC
    In Perl 6 you will be able to do:
    if $value == 1 | 5 | 21 | 99 { ...
    if $value == any( 1, 5, 21, 99) { ...
      In Perl 6 you will be able to...

      And before that comes out (*ducks*), you'll be able use smart matching in perl 5.10:

      use 5.9.5; my $var = shift; my @f = (1,2,3,5,8,13,21); print "fibo!" if $var ~~ @f;

      • another intruder with the mooring in the heart of the Perl

Re: *IN* Operator for the Perl wish list
by Limbic~Region (Chancellor) on Mar 30, 2007 at 12:48 UTC
    Several modules in perl 5 provide this functionality. Currently, Perl 6 will provide set operations and junctions natively. If you do not see how this answers your question I hope you go exploring for more information on your own. You never know what useful information you will find along the way.

    Cheers - L~R

      That can be a hard thing to find in CPAN ... so look behind the readmore for one answer.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://607459]
Approved by lidden
[Discipulus]: the same for me! I'd use your own one! ;=)

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2018-05-28 10:06 GMT
Find Nodes?
    Voting Booth?