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

In most langauages you can say something like:
if $var in "1,2,3,4,5" ...
which will be true if $var is equal to 1,2,3,4, or 5 (or in some languages if the first character of the string $var is 1,2,3,4 or 5). Is there anything like this in perl? I looked around a bit but could not find anything. Thanks

Replies are listed 'Best First'.
Re: Is there an InString-like function?
by simon.proctor (Vicar) on Jul 23, 2001 at 16:11 UTC
    Personally I've not heard of anything like this, however if I needed to do it I'd do it as a sub and pass it an array reference

    if(in($var,$array)) { print "Yes!!!!"; } else { print "No!!!"; } sub in { my $var = shift; my $ref = shift; foreach (@{$ref}) { if($var == $_) { return 1; } } return 0; }


    That should work but its my dinner hour and I'm munching as well :)
      You've heard of grep, right?
      print grep { $_ eq $var } qw( 1 2 3 4 5 ) ? "Found.\n" : "Not found.\n +";
      Note though that grep loops over the entire array everytime, so for large arrays, simon.proctor's function would be better.

      ar0n ]

        Thanks, this is perfect. I was looking for something better than index and you example works great, especially since I am running the test against many multi-lenth strings, not single characters. Unfortunately, the documentation on the "grep BLOCK LIST" is severly lacking so I appreciate your insight. If I ever need to work with long lists, I will use a pattern matching solution like the one given by wiz which also works well.

        Thanks again,
        tigervamp

Re: Is there an InString-like function?
by ariels (Curate) on Jul 23, 2001 at 16:22 UTC
    Look at index. It finds substrings.

    Your example was slightly misleading: index $var, "1,2,3,4,5" >= 0 is not a good way to test if $var is one of these values (as you yourself note). This may have confused some respondents.

Re: Is there an InString-like function?
by Hofmator (Curate) on Jul 23, 2001 at 16:53 UTC

    or in some languages if the first character of the string $var is 1,2,3,4 or 5
    Well, in perl we 'only' have the scalar type, but you can use it as a string. Then a regex might be your solution, TIMTOWTDI. For your example you'd write:
    print '$var starts with 1, 2, 3, 4 or 5' if ($var =~ /^[12345]/); print '$var contains 1, 2, 3, 4 and/or 5' if ($var =~ /[12345]/); print '$var starts with 1, 2, 3, 4 or 5' if ($var =~ /^[12345]/);
    and you can do a lot of further nice things with regexes, have a look at our Tutorials.

    As a remark, this method only works for simple cases, if you want something like $var in (12, 25, 100..233) then a regex is not ideal. Consider the other solutions presented so far for these cases.

    -- Hofmator

Re: Is there an InString-like function?
by mattr (Curate) on Jul 23, 2001 at 17:19 UTC
    I'm a little confused by what you want. The index and grep commands, a regular expression, or querying a hash for the existence of a key are all possible answers.

    In the case of a hash, the key is a (hopefully) unique number calculated from your input so you have to do alphabetic sorting by hand.

        In this case, there is a very easy way of doing this.
        Now all you really have to do it go if ($var =~ /^[12345]/).
        Now if you wanted to do a search through strings, i would suggest going,  if ($var =~ /^[qw(Smith Jones Schmidtz)]/).
        I tested both scripts on Win2k and Active Perl 5.6.1. So basically it works to  if ($variable =~ /^##data to search through here##/). this means if $variable finds that any of the search variables are true (if you don't use ^ then it does an and/or search), it does it's required task.


        Update:
        With help from many people, when doing a string search, drop the [] from a string search, or else your searching through the characters of the string.
        ----------------------------
        Wiz, The VooDoo Doll
        Head Master of 12:30 Productions
        ----------------------------
Re: Is there an InString-like function?
by CharlesClarkson (Curate) on Jul 24, 2001 at 03:48 UTC

    If you feel you may one day port to a quantum computer, you might have the foresight to use the any function in Quantum::Superpositions

    use Quantum::Superpositions; if ( $var == any(1 .. 5) ) {

    HTH,
    Charles K. Clarkson