Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^9: partial matching of lines in perl

by Sidd@786 (Initiate)
on Jun 15, 2020 at 14:26 UTC ( [id://11118096]=note: print w/replies, xml ) Need Help??


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

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;

Replies are listed 'Best First'.
Re^10: partial matching of lines in perl
by AnomalousMonk (Archbishop) on Jun 15, 2020 at 19:24 UTC

    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:  <%-{-{-{-<

Re^10: partial matching of lines in perl
by Sidd@786 (Initiate) on Jun 15, 2020 at 14:27 UTC
    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.

        This prints one blank line (for "bar") ...

        I don't understand this. I get the "quux" line, but I get no blank line. Capturing the output of the grep in an array and then dumping it shows only one item.


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

      #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;
        when i run the above code, it is showing the error-undefined variable $file1.please help me
        $file1 = \<<"END1"; he/is/man/reg[30] what/goes/on/reg[32] i_am_a_man you_are_reg[60] END1 my $file2 = \<<"END2"; /is/man/reg[30] are_reg[60] try/to/do/reg[65] you/reg[31] END2 open(my $f3, ">", "C:/Users/Siddharth/Desktop/two.txt") or die "Can't open < input.txt: $!"; open my $h2, '<', $file2 or die "cannot open file2"; my %map = <$h2>; my ($regex) = map { qr/$_/ } # 2. join '|', map {quotemeta} close $h2; open my $h1, '<', $file1 or die "cannot open file1"; my @a1= <$h1>; #print grep {$_ =~ $regex} @a1; print $f3 grep {$_ !~ $regex} @a1;
          A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11118096]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-25 23:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found