tags:

views:

83

answers:

2

Hi,

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.

+7  A: 

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"

Eric Strom
+7  A: 

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.

ergosys
my code now looks like this: $/ = "\r"; open TEXTFILE, "rankorder/rawdata_2001.text"; while (<TEXTFILE>) { print; }Still no luck though, still only prints the last line
Silmaril89
Sorry that comment is very hard to read(not sure how to format comments correctly). But anyway I added $/ = "\r"; to the top of the perl program, but still no changes. Any other ideas or suggestions?
Silmaril89
chomp; print; print "\n"
ergosys
very nice, thank you
Silmaril89