adhanlon
adhanlon

Reputation: 6539

Why does Perl just give me the last line in the file?

I have downloaded the following file: rawdata_2001.text

and I have the following perl code:

open TEXTFILE, "rawdata_2001.text";
while (<TEXTFILE>) {
    print;
}

This however only prints the last line in the file. Any ideas why? Any feedback would be greatly appreciated.

Upvotes: 1

Views: 822

Answers (2)

ergosys
ergosys

Reputation: 49019

The file is formatted with carriage returns only, so it's being sucked in as one line. You should be able to set $/ to "\r" to get it to read line by line. You then should strip off the carriage return with chomp, and be sure to print a new line after the string.

Upvotes: 7

Eric Strom
Eric Strom

Reputation: 40142

your file probably is using "\r" line endings, but your terminal expects "\n" or "\r\n". try running:

open my $textfile, '<', "rawdata_2001.text" or die;
while (<$textfile>) {
    chomp;
    print "$_\n";
}

you can also experiment with changing the input record separator before the loop with local $/ = $ending;, where $ending could be "\n", "\r\n", "\r"

Upvotes: 6

Related Questions