[email protected]
[Top] [All Lists]

Re: [Za-pm] creating new file one line at a time

Subject: Re: [Za-pm] creating new file one line at a time
From: James E Keenan
Date: Sun, 07 Mar 2010 19:17:04 -0500
(I didn't realize that my first post only went to Anne. So, for the benefit of the list ...)

In Perl, when you read a record like this, you are reading it newline and all. So what we almost always do is to first clear off the newline. Then we analyze/rewrite the line as needed. Then we print it to the write handle with a newline.


while (<IMPORT>) {
  print RESULT "$_|\n";

Note: You are using global filehandles here. Lexically scoped filehandles have been available in Perl since (I think) the introduction of 5.8 in 2002. They're considered "best practice." So what you really want in your 'open' statements is:

open my $IMPORT, '<', "import.txt" ...
open my $RESULT, '>', "delimited.txt"

... which also illustrates the 3-argument form of 'open', which is often (though perhaps not universally) also considered best practice.

And now to follow up further ...

I know that at my $dayjob we often have input of character-delimited records that arrives in DOS format. What that means is that the lines actually end in '\r\n'. One of the other programmers wrote a function called 'chew()' that essentially does this:

$line =~ s/(.*)(\r\n)*/$1/;

In your case, chomp may not be eliminating a final \r, so when you append a pipe it appears on the next line.

Za-pm mailing list
[email protected]

<Prev in Thread] Current Thread [Next in Thread>