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

Re: [MacPerl] find value in array



On Sat, Oct 14, 2000 at 09:40:17PM -0700, Bruce Van Allen wrote:
> At 7:03 PM 10/14/00, g3pb wrote:
> >is there a way other then using foreach to determie if an array contains a
> >particular value. in AppleScript it would be
> >
> >if list contains value then
> >     -- do stuff
> >end if
> >
> 
> 
> 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?

@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.


Ronald

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