我试图用下面的代码读取文件中的行。但是,此代码的结果是打印与文档所在行相同的行。Perl - 读取文件行
open (file_to_rand, "./files/file07.txt") or die "Could not open file";
foreach $line (<file_to_rand>) {
push(@array,$line);
}
close(file_to_rand);
这段代码有什么问题?
我试图用下面的代码读取文件中的行。但是,此代码的结果是打印与文档所在行相同的行。Perl - 读取文件行
open (file_to_rand, "./files/file07.txt") or die "Could not open file";
foreach $line (<file_to_rand>) {
push(@array,$line);
}
close(file_to_rand);
这段代码有什么问题?
如果你只是想读取所有行到数组(这不是有效的大文件):
open my $fh, "<", "./files/file07.txt" or die "Could not open file";
my @lines = readline($fh);
close $fh;
#possible you need to remove new line character at the end of each line:
chomp @lines;
顺便说一句:这是Perl和不使用Perl
除非这个PERL的人们一直在谈论的是一个完全不同的动物,像我们这样的普通人从来没有听说过。 – innaM
是的!哈哈哈 –
那么,什么是这个问题? – devnull
不,这个代码的“结果”是不打印任何东西。这个代码中唯一可以输出任何内容的就是'die'。另外,打印与文档所在行相同的行的语句“”是语法错误,并不代表任何意义。 – TLP