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

#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;

Replies are listed 'Best First'.
Re^12: partial matching of lines in perl
by Sidd@786 (Initiate) on Jun 16, 2020 at 02:39 UTC
    when i run the above code, it is showing the error-undefined variable $file1.please help me

      Update: Ok, you've just changed nodes Re^11: partial matching of lines in perl and Re^12: partial matching of lines in perl to remove matter to which I've just spent a fair amount of time responding. Please don't do that. It's ok to make changes in your posts, but such changes should never be made in such a way as to destroy context. Please see How do I change/delete my post? for site etiquette and protocol regarding changes.


      First, let's talk about formatting PerlMonks posts.

      1. The code here has no closing  </code> tag, so it's almost unreadable. You should have seen it was unreadable when you previewed this post. If you can't read it, neither can we. Please see Writeup Formatting Tips and Markup in the Monastery.
      2. This post contains a mixture of text and data. All code, data, program input/output, and warning and error messages shoud be wrapped (separately!) in  <code> ... </code> tags. Text should be wrapped in  <p> ... your text here ... </p> tags.
      3. These <--> two separate posts pertain to the same problem and/or question. There is no reason for them to be separate. Making them separate adds to the "noise" of the thread and obscures the true relationship of the matter in the posts (and in other pairs of your posts in which you have used this practice). Wise use of  <p> ... </p> and  <code> ... </code> tags will allow you to clearly differentiate the various different matter within a single post.
      Ok, housekeeping done. Now on to practical matters.

      ... help me out and write an executable code

      You already have executable code here! I simply took this code and rewrote it slightly to use file names I thought were more appropriate and a directory local to my development tree, and to use the proper syntax for print-ing to a filehandle, and posted it here, along with the contents of the two input files and the output of the code. This code produced output and correct output insofar as I understood your requirements.

      In response to this post, I took the data posted therein (insofar as I could understand its structure) and put it into files goodfile.txt and badfile.txt as indicated in the post. I then changed my version of your code posted here to use the different file names and data and ran it. The output I saw was the single line
          don't you
      which I understand to be correct: it's the only line in goodfile.txt that doesn't have some line from badfile.txt as a substring. What do you get when you run my code posted here, either with my posted files/data or with your own? Either way, you should get something, not nothing.

      Please carefully review the various steps you have taken to exercise the code you have written and that has been posted in this thread by others. I suspect that, in the midst of a deluge of details, you have overlooked critical steps and are now wandering in desperate confusion. Don't worry; we've all been there. Take a step (or two) back, relax, take a deep breath, enjoy a cookie, center your thoughts, and when your mind has cleared, begin again. Run code that has been posted here with its accompanying data and see if you get the claimed results. If you do, ask how this code/data differs from that you are using. If this approach yields no success, come back and we'll try another tack.


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

      when i run the above code, it is showing the error-undefined variable $file1.please help me

      c:\@Work\Perl\monks\Sidd@786>dir do.txt Volume in drive C is Acer Volume Serial Number is 9840-785B Directory of c:\@Work\Perl\monks\Sidd@786 File Not Found c:\@Work\Perl\monks\Sidd@786>perl #use strict; use warnings; $file1 = 'goodfile.txt'; $file2 = 'badfile.txt'; open $h2, '<', $file2 or die "cannot open file2"; @a2 = <$h2>; close $h2; chomp @a2; $match = join '|', @a2; $match = qr/$match/; open $f3,'>',"do.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; __END__ c:\@Work\Perl\monks\Sidd@786>cat do.txt don't you
      Problem solved!


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

Re^12: partial matching of lines in perl
by Sidd@786 (Initiate) on Jun 17, 2020 at 11:04 UTC
    $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;

      You've been asked several times to make some effort when asking for help, dumping code without a description of what is happening is just lazy.

      Global symbol "$file1" requires explicit package name (did you forget +to declare "my $file1"?) at sid.pl line 5. Can't find string terminator "END1" anywhere before EOF at sid.pl line + 5.

      See previous notes on use strict; use warnings;.

      open(my $f3, ">", "C:/Users/Siddharth/Desktop/two.txt") or die "Can't open < input.txt: $!";

      two.txt and input.txt aren't the same file, your report is wrong as you report a reading failure when you're actually trying to write a file. Take the time to read and understand the responses and working examples you've already been given.

      Are you sure you want to map quotemeta to the return value of close?

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      use strict; use warnings; my %ads_list_hash=(); my $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 < two.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.