For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > July 2006 > Re: How to split a file on the CR carriage Return and/or replacethe









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 replacethe
Rob Dixon

2006-07-26, 6:57 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)
> {


Your code is laid out a bit obscurely Steve. Unpacking it a little and taking
out some unnecessary stuff it looks like this:


open IN, $_[0] or die "Can't open DAT source file: $tempFile $!\n";

while (<IN> ) {
s/\r/\n/g;
@lines = <IN>;
}

close (IN);


Is this code within a subroutine? Because if not then your @_ array shouldn't be
populated and you will have no filename. Also, what's $tempFile? However,
presumably your file open is succeeding, so..

The while loop only executes once. It will read the first record from the file
into $_, change all "\r" into "\n" in that record, throw it away, and read the
rest of the file into @lines. IN is now at eof so the loop will terminate.

I'm worried that you expect carriage returns terminating your records, yet you
say your file is a Unix file. Unix traditionally has lone linefeeds terminating
text records, whereas Windows has carriage return/linefeed pairs. But Perl
should hide either format from you and give you records ending in just "\n".
Records terminated by carriage return is the Mac standard, and you can get
around this by setting the $/ variable like this:

sub routine {

my $file = shift;
open my $in, $file or die "Can't open DAT source file: $file $!\n";
my @lines;
{
local $/ = "\r";
chomp(@lines = <$in> );
}

foreach my $line (@lines) {
:
:
}
}

But please be sure that the data is really formatted as you think it is.

Also, please use strict and warnings, and declare all of your variables - it
saves an enormous amount of time debugging simple coding errors.

HTH,

Rob
Sponsored Links







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

Copyright 2008 codecomments.com