-1

I'm trying to read the lines on a file with the code below. But the result of this code is to print the same line as lines the document has.

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);

What is wrong with this code?

4

1 回答 1

3

如果您只想将所有行读入数组(这对大文件无效):

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

于 2013-07-13T10:53:01.020 回答