in reply to Is list member

couldnt you use grep to do this ?

#!/usr/bin/perl -w # # quick test to match an item in an array use strict; my $match = shift ; my @arr = qw( 2 3 4 5 6 7 foo foo12 12foo 1foo1) ; print "match \n" if ( grep(/\b$match\b/, @arr) ) ;

1 as input will not match
foo as input will not match
2 as input matches!

works for me.. is this a bad way to go about it ?
my regex may not be optimal. regex is not my strong point. I just got the book on it. It's next after "Data Munging with perl"

Replies are listed 'Best First'.
Re: Grep ?..
by demerphq (Chancellor) on May 06, 2002 at 17:16 UTC
    Personally I would say that as long as the intention is find a single element in a list then grep is not the ideal. grep is more useful when you want to find all the elements in a list that match some criteria. Using it to find one element usually means that grep is doing a whole lot more work than is necessary. (It wont stop once its found the element even if it were the first.)

    Yves / DeMerphq
    ---
    Writing a good benchmark isnt as easy as it might look.