Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Iterator to parse multiline string with \\n terminator

by CountZero (Bishop)
on Oct 06, 2013 at 07:42 UTC ( #1057127=note: print w/ replies, xml ) Need Help??


in reply to Iterator to parse multiline string with \\n terminator

If your file is not terribly huge and/or you have enough memory, this is an alternative solution:

use Modern::Perl; my $file; { local $/ = ''; $file = <DATA>; } $file =~ s/\\\n/ /gs; my @lines = split /\n/, $file; say for @lines; __DATA__ First line Second line (part1)\ Second line (first continuation)\ Second line (second continuation) Third line (part1)\ Third line (first continuation)\ Third line (second continuation) Fourth line (part1)\ Fourth line (first continuation)\ Fourth line (second continuation)
Output:
First line Second line (part1) Second line (first continuation) Second line (seco +nd continuation) Third line (part1) Third line (first continuation) Third line (second +continuation) Fourth line (part1) Fourth line (first continuation) Fourth line (seco +nd continuation)

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

My blog: Imperial Deltronics


Comment on Re: Iterator to parse multiline string with \\n terminator
Select or Download Code
Re^2: Iterator to parse multiline string with \\n terminator
by three18ti (Scribe) on Oct 06, 2013 at 09:21 UTC

    Thanks!

    At the moment I'm only parsing a file that's a few megs / few thousands lines, however, I have the potential to be parsing a lot larger files so memory could become a problem in the future.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1057127]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2014-09-18 19:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (121 votes), past polls