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

Also we have to operate with only file locations in program,we are not supposed to write actual texts of file in program(referring to the same problem) and we have to also print the output in other file.
  • Comment on Re^5: partial matching of lines in perl

Replies are listed 'Best First'.
Re^6: partial matching of lines in perl
by AnomalousMonk (Archbishop) on Jun 15, 2020 at 11:05 UTC
    ... operate with only file locations ... not ... texts of file in program ...

    Opening files for reading in the "normal" way (as opposed to the ramdisk-ish approach used for convenience in the example code) is straightforward and is well described in the open and perlopentut docs. Maybe also take a look at Files and I/O in perlintro.

    ... print the output in other file.

    This merely involves opening a write filehandle (see docs referred to above) inside the Perl script and print-ing to this filehandle rather than to STDOUT by default, or else redirecting standard output from the Perl script as a whole to a file via the OS command line (see your OS docs regarding I/O redirection).

    Update: Minor wording changes to (hopefully) enhance clarity.


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

Re^6: partial matching of lines in perl
by soonix (Chancellor) on Jun 15, 2020 at 11:35 UTC
Re^6: partial matching of lines in perl
by Sidd@786 (Initiate) on Jun 15, 2020 at 13:40 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;

    2020-06-21 Athanasius added code tags.

      please tell me the problem occuring in this program,because it is giving nothing in the output file.Input file contents are same as told earlier.

        You have

        use warnings;

        at the top of your program.

        When you run your program, Perl will tell you at least one problem in your program immediately:

        perl -wle 'open my $fh, "<", "test.txt"; print $fh "hello"' Filehandle $fh opened only for input at -e line 1.

        If you don't get this output, please either post the real code you are using or find a way to see the output of your program.