sk35 has asked for the wisdom of the Perl Monks concerning the following question:

im having 2 files . i want to compare contents line by line and delete the matching ones. im able to do that with the below section. my issue is if i use a wild card in 1 file to match mulitple lines in second file , im not able to delete mulpitple matches

use strict; use warnings; $\="\n"; open my $fh1, '<', 'file1' or die $!; open my $fh2, '<', 'file2' or die $!; open my $out, '>', 'file1minusfile2' or die $!; chomp(my @arr1=<$fh1>); chomp(my @arr2=<$fh2>); foreach my $x (@arr1){ print $out $x if (!grep (/^\Q$x\E$/,@arr2)); }

Replies are listed 'Best First'.
Re: compare contents of 2 file having wildcards
by choroba (Cardinal) on May 19, 2016 at 11:41 UTC
    If file1 contains regular expressions, not literal strings, don't use \Q...\E.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: compare contents of 2 file having wildcards
by Laurent_R (Canon) on May 19, 2016 at 11:42 UTC
    Can you please show what you mean by "using a wild card in one file to match multiple lines in the second file"?

    A regex "wild card"? Something else? This is not clear to me.

    As a side note, please note that using a hash might be much more efficient than scanning an entire array for each record of the other array. But, of course, that would probably not be possible with a wild card.

Re: compare contents of 2 file having wildcards
by Anonymous Monk on May 19, 2016 at 11:41 UTC
    One issue: The \Q...\E is preventing the contents of $x from being interpreted as a regex. See quotemeta and Mind the meta!