Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: line ending troubles

by bobf (Monsignor)
on Dec 21, 2009 at 23:51 UTC ( #813773=note: print w/ replies, xml ) Need Help??


in reply to line ending troubles

I ran into a similar problem a while ago. You may find the solutions presented in Newlines: reading files that were created on other platforms to be helpful. The two main options that I considered were:

  1. Use a regex to match the newline character(s) in the file. I think this would require slurping the whole file and then doing something like if( $file =~ m/\015$/ ) (which assumes the file will end with a newline) or if( $file =~ m/\015(?!\012)/ ) (which doesn't), setting $/ according to what matched, and re-reading the file line-by-line. If the file contains different types of newlines this will not work.
  2. Preprocess the input file to convert all newline characters to the current system's newline character.
    $file =~ s[(\015)?\012(?!\015)][\n]g; $file =~ s[(\012)?\015(?!\012)][\n]g;
I used the latter option. It worked beautifully.


Comment on Re: line ending troubles
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (16)
As of 2015-07-07 17:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (92 votes), past polls