[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] find value in array



On Sun, 15 Oct 2000 04:11:38 -0500, Terry Romine wrote:

>>         if(grep { $_ eq $item } @list) {
>>             print "List contains $item!\n";
>>         }

>Does this "do stuff" to each element in the array that fits the test?

No. grep() counts the number of items that fit the test, because it's
used in a scalar context. In a list context, you get a list of items
that fit the test:

 	@list = qw(a b a a a b a b b a);
	foreach (grep { $_ eq 'b' } @list) {
	    print "Ding!\n";
	}

or, using map:

	@result = map { "Ding!" } (grep { $_ eq 'b' } @list;
	print "@result\n"

-- 
	Bart.

# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org