in reply to search for blank lines by RegEx

My naiive approach (I'm a newbie) would be to check two lines in a row, something like...

my $prev; while (<DATA>) { if (/^\s*$/ && $prev =~ /^\s*$/) { # Two blank lines in a row found } $prev = $_; }

It doesn't look a particularly Perl way to do it though, so I guess my inexperience is showing.

Replies are listed 'Best First'.
Re^2: search for blank lines by RegEx
by Aragorn (Curate) on Sep 07, 2004 at 09:02 UTC
    For processing a huge file, this is a lot more (memory-) efficient than trying to load the complete file into memory. So I don't think this is a bad way to do it.

    Arjen

      The question asked for a regex solution. If you want to deviate and use a loop, there's no need to use a couple of regexes inside the loop. Something like (untested):
      perl -lne 'die "Line ", $.-2, "\n" if ($e = length() ? 0 : $e+1) == 2'

        Fair point about being faithful to the question -- and thanks for the alternate approach (above my head at the moment, but I'll crack out the documentation to find out...), but I do remember reading somewhere or other about asking questions in ways that don't prejudice the answers.

        Still, this was an interesting question for the level of experience I have in Perl.