in reply to how to next line after the pattern in perl

A simple state machine will work:
use strict; use warnings; my $flag = 0; while (<DATA>) { if (/XXXX/) { $flag = 1; } elsif ($flag) { print; $flag = 0; } } __DATA__ ZZZZZZZZZZZZ YYYYY XXXX XX XXXX 3333 ii XXXX asdfddd

Replies are listed 'Best First'.
Re^2: how to next line after the pattern in perl
by kennethk (Abbot) on Jun 09, 2010 at 19:22 UTC
    And a version that will catch consecutive lines and is shorter. Shorter is always better, right?

    use strict; use warnings; my $flag = 0; while (<DATA>) { print if $flag; $flag = /XXXX/; } __DATA__ ZZZZZZZZZZZZ YYYYY XXXX XX XXXX 3333 ii XXXX asdfddd

    Correct, but fundamentally a repeat of Re: how to print next line after the pattern in perl.

      "...catch consecutive lines...?
      and
      Correct but fundamentally....

      Interesting proposition which points up a (possible) ambiguity or shortcoming in the original problem statement: What should be done when a second consecutive matching line exists?

      • Print it and the following line? (which yours does)
      • Print just the non-matching line?
      • Something else?

      Update: Fixed punct at the end of the first li from "?" to ")"
      while LOL at kennethk's reply

        Perhaps an appropriate solution?

        use strict; use warnings; my $flag = 0; while (<DATA>) { print if $flag; if ($flag and /XXXX/) { eval('$demons->fly_out($nose)'); } $flag = /XXXX/; } __DATA__ ZZZZZZZZZZZZ YYYYY XXXX XX XXXX 3333 ii XXXX asdfddd