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.
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.
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"
精彩评论