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

Re: [MacPerl] find value in array



On Sun, Oct 15, 2000 at 09:59:18PM -0700, Bruce Van Allen wrote:

> Here's a subroutine that will work for all cases as long as the 
> character '~' isn't expected in the data:
> 
> sub inlist {
>    my ($value, @list) = @_;
>    $_ = join('~', @list);
>    return unless /^($value)~/ || /^(.*~$value)~/ || /^(.*~$value)$/;
>    my $chunk = $1;
>    \($chunk =~ tr/~/~/)
> }
> 
> Can the pattern matching could be optimized? I used the || syntax to 
> be sure to get the right thing in $1.


What if you added a ~ to the beginning and end of the string?


sub inlist {
    my($value, @list) = @_;
    local($_) = '~' . join('~', @list) . '~';

    return unless /^(.*?~$value)~/;

    my $chunk = $1;
    $chunk =~ tr/~// or '0 but true';  # :)
}


Ronald

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