This is a faq: see How do I compute the difference of two arrays? How do I compute the intersection of two arrays? in perlfaq4.
Assuming no special characters in your inputs, you could repair your code by using the special variable $" - it controls how arrays are interpolated into strings. Something like:
@array = <FILE>; @pattern = <FILE2>; local $" = '|'; foreach $line(@array) { if ($line =~ m/@pattern/) { print $line; } }
| functions as an or in regular expressions; see Metacharacters in perlre. If your input may contain regular expression metacharacters, you can use \Q and \E with a map to escape those and accomplish the same thing (see Quote and Quote like Operators in perlop):
@array = <FILE>; @pattern = map "\Q$_\E", <FILE2>; local $" = '|'; foreach $line(@array) { if ($line =~ m/@pattern/) { print $line; } }
Finally, please wrap code in <code> tags to preserve formatting and make downloading code easier for monk who wish to help; see Writeup Formatting Tips.
In reply to Re: comparing two arrays and displaying scalar matches
by kennethk
in thread comparing two arrays and displaying scalar matches
by pipeops
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |