Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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 ( #765765=note: print w/replies, xml ) Need Help??


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

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://765765]
help
Chatterbox?
[Eily]: marto I clearly don't have the thread knowledge to understand the importance of that example
[choroba]: the related discussion on Hacker news is interesting
[choroba]: not so many Perl haters so far

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (11)
As of 2017-07-26 14:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I came, I saw, I ...
























    Results (396 votes). Check out past polls.