http://www.perlmonks.org?node_id=1031539


in reply to Re: Regex and writing lines in file
in thread Regex and writing lines in file

Yes there is one line where we can do match out of the three lines and that line is the first line in three lines.

Replies are listed 'Best First'.
Re^3: Regex and writing lines in file
by BillKSmith (Monsignor) on May 01, 2013 at 18:45 UTC
    The following code uses an array as a "circular list" to delay the printing of the EXTRA. Is this what you need?
    use strict; use warnings; my $match = qr /Match/; my $extra = qq(EXTRA\nEXTRA\n); my @extras = (q()) x 3; my $in = 3; my $out = 0; while (my $line = <DATA>) { $out = ($out+1) % 4; print $line, $extras[$out]; $in = ($in +1) % 4; $extras[$in] = ($line =~ /$match/)? $extra : q(); } __DATA__ a b Match1 Match2 c d e f g Match3 h i j k
    Bill