in reply to Is there an InString-like function?

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 :)

Replies are listed 'Best First'.
(ar0n) Re (2): Is there an InString-like function?
by ar0n (Priest) on Jul 23, 2001 at 16:19 UTC
    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