Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Parsing multiline string line by line

by olus (Curate)
on Feb 19, 2009 at 12:41 UTC ( #745023=note: print w/ replies, xml ) Need Help??


in reply to Parsing multiline string line by line

You can split the string on newlines into an array, and then iterate through it in a foreach loop.

my @lines = split /\n/, $str; foreach my $line (@lines) { ... }


Comment on Re: Parsing multiline string line by line
Select or Download Code
Re^2: Parsing multiline string line by line
by zwon (Monsignor) on Feb 19, 2009 at 14:15 UTC

    Note that this will remove LF in the end of the strings, but not CR, so if newlines in DOS format you will have CR in the end of the line, and it wouldn't work with old Mac strings at all. I personally prefer the following variation:

    for (split /^/, $str) { ... }

    It works just like while (<>)

    Updated: fixed, thanks to ikegami

      split /\n/ and split /^/ both run fine on Windows and old Macs, for two different reasons

      In Windows, the CRLF gets converted to LF on read, so there's no CR in the string to split.

      In old Macs, \n is redefined to CR.

      If you meant there would be problems parsing files from one system on a different system, you need to add unix files to the list.

      [split /^/] works just like while (<>)

      Indeed. It keeps the trailing newlines, and it keeps trailing blanks lines. split /\n/ does neither.

Re^2: Parsing multiline string line by line
by flamey (Scribe) on Feb 19, 2009 at 14:15 UTC
    yeah, I was actually hoping to avoid doing this, just probably didn't make it clear in the question. but I do appreciate the reply :-) thanks!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (16)
As of 2014-07-25 20:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (175 votes), past polls