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

Re: [MacPerl] find value in array



On Sun, 15 Oct 2000 09:58:21 -0400, Ronald J Kimball wrote:

>> There's nothing quite as simple as that, but you can collapse the 
>> array into a string and check for a pattern match:
>> 
>> if (join('', @list) =~ /$value/) {
>>    # do stuff
>> }
>> 
>> HTH
>
>What if /$value/ could match the end of one element and the beginning of
>the next?

What if it can match the middle of a string? For example, "ap" in
"pineapple"? What if $value contains a metacharacter?

>@list = qw/ house fish flower train bowl apple /;
>$value = 'rainbow';
>
>@join("\0", @list) would avoid that problem, as long as $value can't match
>a substring containing a null character.

If $value and the items can't contain a newline (CR on a Mac), there's
another possibility:

	@list = qw/house fish flower train bowl apple/;
	foreach my $value (qw'rain train house apple') {
	    local $" = "\n";
	    if("@list" =~ /^\Q$value\E$/m) {
	        print "The list contains $value\n";
	    }
	}

This gives only positive responses for 'train', 'house' and 'apple'.
	
-- 
	Bart.

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