For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > July 2006 > Re: How to split a file on the CR carriage Return and/or replace the \r with \n ??









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: How to split a file on the CR carriage Return and/or replace the \r with \n ??
Robert Citek

2006-07-26, 6:57 pm


On Jul 26, 2006, at 2:59 PM, Steve Pittman wrote:
> I am using activestate on a windows box ...the files I am parsing are
> Unix files...I tried this so far...
>
> open ( IN, "<$_[0]" )||die "Can't open DAT source file: $tempFile
> $!\n";
> while (<IN> ){s/\r/\n/g;@lines = <IN>;}#
> close (IN);
>
> foreach $line (@lines)
> {


Try a different value for the INPUT_RECORD_SEPARATOR ($/):

$/="\n" ;
open ( IN, "<$_[0]" ) or die "Can't open DAT source file: $tempFile $!
\n" ;
my @lines = <IN> ;
foreach $line (@lines)
{ ...

Notice that I also changed the || to "or".

Let us know what you try and how it works for you.

Regards,
- Robert
http://www.cwelug.org/downloads
Help others get OpenSource software. Distribute FLOSS
for Windows, Linux, *BSD, and MacOS X with BitTorrent


Dr.Ruud

2006-07-28, 9:56 pm

Robert Citek schreef:

> Try a different value for the INPUT_RECORD_SEPARATOR ($/):
>
> $/ = "\n" ;


(spaces added for clarity)

Is that different from the default?
(see perlvar)

--
Affijn, Ruud

"Gewoon is een tijger."


Xavier Noria

2006-07-28, 9:56 pm

On Jul 28, 2006, at 9:50 PM, Joshua Colson wrote:

> On Fri, 2006-07-28 at 21:28 +0200, Dr.Ruud wrote:
>
> Using ActiveState on Windows it is.


That's wrong.

$/ is eq "\n" everywhere, and "\n" has length 1 everywhere. "\n" is
eq "\012" in all systems except MacOS pre-X where it is eq "\015". In
particular "\n" is *not* CRLF on Windows.

My article about newlines is moving forward at O'Reilly, I hope I can
soon provide a pointer to it. Misconceptions about newlines arise too
often, which was my main motivation to write it.

-- fxn

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com