开发者

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

开发者 https://www.devze.com 2022-12-16 10:27 出处:网络
I have downloaded the following file: rawdata_2001.text and I have the following perl开发者_运维技巧 code:

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"

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号