in reply to matching strings...

Why not use a list operator in this case? grep comes to mind, but especially first that is in the List::Util module (part of the Perl 5.8 distribution if memory serves).

use List::Util qw( first ); if (defined(first {'abc' eq $_} qw( abc def ghi ))) { print "found 'abc'\n"; }

Hope this helps, -gjb-

Update: Thanks to diotalevi for pointing out issues with my solution. I agree with both: I suggested first for exactly his objection to grep and I modified the original code by adding defined to resolve his objection to find.

Replies are listed 'Best First'.
Re^2: matching strings...
by diotalevi (Canon) on Jan 27, 2003 at 08:32 UTC

    Both of these are poor choices. You wouldn't use grep because that's a wasteful way to program. Not only does it not stop after finding a match, it goes to all the trouble to create a copy of the matching elements in memory. The first() function is also poor because that assumes that the data itself evaluates as a 'true' value. That doesn't work when your data is things like "" and 0.


    Seeking Green geeks in Minnesota