Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Split on Blank Line into array, and keep the blank line in the array

by RBBD (Initiate)
on May 22, 2009 at 21:55 UTC ( [id://765762]=perlquestion: print w/replies, xml ) Need Help??

RBBD has asked for the wisdom of the Perl Monks concerning the following question:

The following splits on blank lines, but they are lost.

How do I keep them in the output?
#!/usr/bin/perl #<"Grep.pl"> Grep Profile Info File undef $/; $INFOFILE="/admin/users/diebelr/llProfile.out"; open (IFILE, "$INFOFILE") || die ("Cannot open $INFOFILE\n"); @finfo=split(/\n{2,}/, <IFILE>); #<+++++ $tRecsInfo = @finfo; print "$tRecsInfo"; print "I read ", scalar(@finfo), " RECORDS\n"; @MATCHING = (); @MATCHING = grep(/\b@ARGV[0]\b/, @finfo); $MATCHED = @MATCHING; print "@MATCHING"; print "\n$MATCHED Entries Matched\n";
  • Comment on Split on Blank Line into array, and keep the blank line in the array
  • Download Code

Replies are listed 'Best First'.
Re: Split on Blank Line into array, and keep the blank line in the array
by johngg (Canon) on May 22, 2009 at 23:15 UTC

    It is difficult to know what you intend from your code without sight of the data but as a general answer, if you use parentheses in the split regular expression to capture what you split on, it will be preserved in the resultant list or array.

    $ perl -le ' > $str = q{abcDEfghIjkLmn}; > @arr = split m{([A-Z]+)}, $str; > print for @arr;' abc DE fgh I jk L mn $

    I hope this is helpful.

    Cheers,

    JohnGG

      The source file has a \n at the end of the last line of each section, plus another \n to create the blank line. I wish the content of the array to be identical. I have read much about parentheses to preserve the delimiter, but have not been able to get that to work with the blank line delimiter. All help much appreciated...

        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

Re: Split on Blank Line into array, and keep the blank line in the array
by linuxer (Curate) on May 22, 2009 at 22:15 UTC

    Please use code tags. See Markup in the Monastery for details.

    How do you want to treat the blank lines?

    Maybe you need to read blockwise?

    #!/usr/bin/perl -l use strict; use warnings; { local $/ = ""; my @blocks = <DATA>; local $, = "\n"; print map { ">>>$_<<<" } @blocks; } __DATA__ AB A BB CC C CD EE EA

    Maybe you want to capture the split pattern?

    #!/usr/bin/perl -l use strict; use warnings; my $content = do { local $/; <DATA> }; my @blocks = split /(\n\n+)/, $content; { local $, = "\n"; print map { ">>>$_<<<" } @blocks; } __DATA__ AB A BB CC C CD EE EA
Re: Split on Blank Line into array, and keep the blank line in the array
by ig (Vicar) on May 23, 2009 at 04:58 UTC

    If I understand you correctly, I think what you want is the following:

    @finfo=split(/(\n{2,})/, &lt;IFILE>); #&lt;+++++

    From split:

    If the PATTERN contains parentheses, additional list elements are created from each matching substring in the delimiter.

    split(/([,-])/, "1-10,20", 3);

    produces the list value

    (1, '-', 10, ',', 20)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://765762]
Approved by johngg
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-19 19:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found