This may do what you want. It uses a positive look-behind assertion so that the pattern splits on multiple newlines as long as thet are preceded by a newline. There ia also a look behind to cope with bank lines at the start of your data in case you need that. If you do, there will be an empty record at the start of your array that you can shift away.

use strict; use warnings; my $startOffset = tell DATA; my $recordCt; my @records = do { local $/; split m{(?x) (?: (?<=\A) | (?<=\n) ) (\n+) }, <DATA>; }; $recordCt = 0; print qq{@{ [ sprintf qq{\n%2d >}, ++ $recordCt ] }$_<} for @records; __END__ Record 1 consists of three lines of data terminated by a newline Record 2 has just two lines terminated by a newline Record 3 contains four lines of data which is very important to the project, newline terminated again Record 4 was preceded by two blank lines and has two lines terminated by a newline

The output.

1 >Record 1 consists of three lines of data terminated by a newline < 2 > < 3 >Record 2 has just two lines terminated by a newline < 4 > < 5 >Record 3 contains four lines of data which is very important to the project, newline terminated again < 6 > < 7 >Record 4 was preceded by two blank lines and has two lines terminated by a newline <

Note that records 2, 4 and 6 are the newlines we preserved with the regex captures and each record finishes with a newline as you can see from the positions of the '<'s.

I hope this is helpful.

Cheers,

JohnGG


In reply to Re^3: Split on Blank Line into array, and keep the blank line in the array by johngg
in thread Split on Blank Line into array, and keep the blank line in the array by RBBD

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.