in reply to Parsing a file to print multiple times

But not able to achive the same.

What do you achieve?

  • Comment on Re: Parsing a file to print multiple times

Replies are listed 'Best First'.
Re^2: Parsing a file to print multiple times
by ExperimentsWithPerl (Acolyte) on Sep 26, 2015 at 08:05 UTC



    From the first code I am getting the output :-


    print from this line 1
    lets print the following line too 1
    Okay lets close it now 1
    closing

    And from the second code I am getting the output :-


    print from this line 1
    lets print the following line too 1
    Okay lets close it now 1
    closing at this line 1
    this was fine
    closing at this line 1
    but now is the problem
    2nd line to ignore
    print from this line as well 2
    lets print the following line too 2
    Okay lets close it too now 2
    closing at this line 2
    No I dont want to print this line
    closing at this line 2
    bye

      Try to fill in the blanks (...) to this
      my @nums = 1 .. 10; my $start = 3; my $end = 5; my $go = 0; for my $num ( @nums ){ ... if( $go ){ print "$num\n"; } }

        Thanks a lot Anonymous Monk.You rock!!!
        This logic worked.Below is the final code :-

        open (FILE, '<', 'sample.txt') or die "Could not open sample.txt: +$!"; open (FILE1, ">output1.txt") or die "Could not open sample.txt: $! +"; my @nums = <FILE>; my $start = "print from"; my $go = 0; my $sizeconf = scalar @nums; for my $num ( @nums ){ if ($num =~ /$start/) { $go = 1; } if($go){ print FILE1 "$num"; } my $end = "closing"; if ($num =~ /$end/ && $num !~ /$start/) { $go = 0; } }


        The last point of "$num =~ /$end/ && $num !~ /$start/" because in my actual input file $start conatins the staring $end.

        I will intergrate this logic with my tool and update you.