in reply to Re^7: partial matching of lines in perl
in thread partial matching of lines in perl

This node was taken out by the NodeReaper on Jun 16, 2020 at 11:40 UTC
  • Comment on Reaped: Re^8: partial matching of lines in perl

Replies are listed 'Best First'.
Re^9: partial matching of lines in perl
by Sidd@786 (Initiate) on Jun 15, 2020 at 14:26 UTC
    use strict; use warnings; my $file1 = 'C:/Users/Siddharth/Desktop/goodfile.txt'; my $file2 = 'C:/Users/Siddharth/Desktop/badfile.txt'; open my $h2, '<', $file2 or die "cannot open file2"; my @a2 = <$h2>; close $h2; chomp @a2; my $match = join '|', @a2; $match = qr/$match/; open my $f3,'>',"C:/Users/Siddharth/Desktop/do.txt" or die "$!"; open my $h1, '<', $file1 or die "cannot open file1"; my @a1 = <$h1>; close $h1; #my $fh = grep {$_ !~ $match} @a1; #print $f3 $fh; print grep {$_ !~ $match} @a1;

      If I have the input files
      file1.txt:

      1.he is man 2.don't you 3.xyzzy 4.what goes on
      and
      file2.txt:
      he is z what are try to do
      and I run the script
      pm_11118096_1.pl:
      # pm_11118096_1.pl 15jun20waw use strict; use warnings; my $file1 = 'file1.txt'; my $file2 = 'file2.txt'; open my $h2, '<', $file2 or die "cannot open file2"; my @a2 = <$h2>; close $h2; chomp @a2; my $match = join '|', @a2; $match = qr/$match/; open my $f3,'>',"do.txt" or die "$!"; open my $h1, '<', $file1 or die "cannot open file1"; my @a1 = <$h1>; close $h1; print grep {$_ !~ $match} @a1; # print $f3 grep {$_ !~ $match} @a1;
      I get the following output:
      c:\@Work\Perl\monks\Sidd@786>perl pm_11118096_1.pl 2.don't you 4.what goes on

      If I comment out the
          # print grep {$_ !~ $match} @a1;
      line and uncomment the
          print $f3 grep {$_ !~ $match} @a1;
      line, I get an output file do.txt with the content

      2.don't you 4.what goes on
      (and no output in the command window).

      When you run your code with these input files, what do you get?


      Give a man a fish:  <%-{-{-{-<

      it is again printing nothing

        Perhaps your algorithm is wrong or perhaps your data isn't what you think it is. Here is an SSCCE:

        #!/usr/bin/env perl use strict; use warnings; my @a2 = qw/foo bar baz/; my $match = join '|', @a2; my @a1 = qw/bar quux/; print grep {$_ !~ $match} @a1;

        This prints one blank line (for "bar") and one line with "quux" in it. Do you understand why?

        Amended thanks to AnomalousMonk for pointing out the error.

        #use strict; use warnings; $file1 = 'C:/Users/Siddharth/Desktop/our.txt'; $file2 = 'C:/Users/Siddharth/Desktop/my.txt'; open $h2, '<', $file2 or die "cannot open file2"; @a2 = <$h2>; close $h2; chomp @a2; $match = join '|', @a2; $match = qr/$match/; open $f3,'>',"C:/Users/Siddharth/Desktop/one.txt" or die "$!"; open $h1, '<', $file1 or die "cannot open file1"; my @a1 = <$h1>; close $h1; #print grep {$_ !~ $match} @a1; print $f3 grep {$_ !~ $match} @a1;
Re: Reaped: Re^8: partial matching of lines in perl
by Sidd@786 (Initiate) on Jun 16, 2020 at 13:00 UTC

    referring to the previous scenario if my input file has texts along with numbers and special characters then how this partial mismatching is done.

    file1 contents: 1. Ram/is/going/reg[30] 2. he/is/coming/reg[31] 3. + what/goes/on/reg[32] file2 contents: 1. is/coming/reg[31] 2. on/reg[32] 3. try/to +/do/reg[65] output file contents: Ram/is/going/reg[30]

      ... special characters ...

      I'm not sure what this thread is anymore (if it even really exists), but the handling of regex metacharacters or "special" characters is covered in haukex's Building Regex Alternations Dynamically article which I originally referenced here. Please read and understand it. And what hippo posted.


      Give a man a fish:  <%-{-{-{-<

      use strict; use warnings; my $file1 = \<<"END1"; he/is/man/reg[30] don't/you/reg[31] what/goes/on/reg[32] END1 my $file2 = \<<"END2"; /is/man/reg[30] on/reg[32] try/to/do/reg[65] END2 open(my $f3, ">", "C:/Users/Siddharth/Desktop/do.txt") or die "Can't open < input.txt: $!"; open my $h2, '<', $file2 or die "cannot open file2"; my @a2 = <$h2>; close $h2; chomp @a2; my $match = join '|', @a2; $match = qr/$match/; open my $h1, '<', $file1 or die "cannot open file1"; my @a1 = <$h1>; close $h1; my $fh = grep {$_ !~ $match} @a1; print $f3 $fh; #print grep {$_ !~ $match} @a1;
      A reply falls below the community's threshold of quality. You may see it by logging in.
    A reply falls below the community's threshold of quality. You may see it by logging in.
A reply falls below the community's threshold of quality. You may see it by logging in.