in reply to Re: Array compare using elements as the pattern match.
in thread Array compare using elements as the pattern match.

++, just a small note: if @array1 contains strings to be matched exactly instead of regular expressions, the pattern should be written /\Q$pat\E/ (quotemeta)

Replies are listed 'Best First'.
Re^3: Array compare using elements as the pattern match.
by RonW (Parson) on Dec 12, 2014 at 22:23 UTC

    Alternately:

    for (@array2) { for my $pat (@array1) { if (index($_,$pat) > -1) { push @a3, $_; last; } } }

    For exact matches, index will be faster.

    A more Perl-ish way:

    for my $pat (@array1) { push @a3, grep { index($_,$pat) > -1 } @array2; }
      Why do you consider that a "more Perl-ish way"? Your grep traverses the entire array every time, whereas my for/last only traverses until it finds a match.