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

use warnings; use strict; use Data::Dumper; my @array1 = qw(234 453 111 239); my @array2 = qw(v204_txt v234_txt v450_txt v453_txt); my @a3; for (@array2) { for my $pat (@array1) { if (/$pat/) { push @a3, $_; last; } } } print Dumper(\@a3); __END__ $VAR1 = [ 'v234_txt', 'v453_txt' ];

Replies are listed 'Best First'.
Re^2: Array compare using elements as the pattern match.
by Anonymous Monk on Dec 12, 2014 at 21:43 UTC
    ++, 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)

      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.
Re^2: Array compare using elements as the pattern match.
by Anonymous Monk on Dec 12, 2014 at 21:26 UTC
    Nice and simple...
    I should've been able to come up with that....arrrrggg my brain hurts today.
    Thank you so much.