将文件行存储到数组中
Storing lines of file into array
所以我对 Perl 还很陌生,才学了 1 周。我试图只将特定范围的行读入数组。如果我在 if 语句中打印 $_,它会准确列出我想要存储到我的数组中的内容。但是将 $_ 存储到我的数组中,然后在 while 之外打印 @array 什么也没有显示。我不确定我应该做什么。我试图将它存储到一个数组中的原因是太从列中获取某些信息,因此需要一个数组来这样做。谢谢你的帮助。对你们来说可能真的很简单
use strict;
use warnings;
my $filename = 'info.text';
open my $info, $filename or die "Could not open $filename: $!";
my $first_line = 2;
my $last_line = 15;
open(FILE, $filename) or die "Could not read from $filename, program halting.";
my $count = 1;
my @lines;
while(<FILE>){
if($count > $last_line){
close FILE;
exit;
}
if($count >= $first_line){
#print $_;
push @lines, $_;
}
$count++;
}
print @lines;
Perl 实际上有一个变量,$.
,表示最近使用的文件句柄的当前行:
来自perldoc -v $.
:
HANDLE->input_line_number( EXPR )
$INPUT_LINE_NUMBER
$NR
$.
Current line number for the last filehandle accessed.
Each filehandle in Perl counts the number of lines that have been read
from it. (Depending on the value of $/ , Perl's idea of what
constitutes a line may not match yours.) When a line is read from a
filehandle (via readline() or <> ), or when tell() or seek() is called
on it, $. becomes an alias to the line counter for that filehandle.
您可以使用这个变量来大大简化您的代码:
use strict;
use warnings;
my $filename = 'info.text';
open(FILE, $filename)
or die "Could not read from $filename, program halting.";
my @lines;
while(<FILE>){
next unless $. >= 2 && $. <= 15;
push @lines, $_;
}
close FILE;
print @lines;
您可以将此代码或修改后的版本包装在接受文件句柄、起始行和结束行的子例程中,以使其更加灵活。
另外一个跟你手头的问题无关的说明,推荐你always use three argument open。
用数字表示更简单:
my @lines = (<FILE>)[1..14];
(注意 - perl
数组从零开始 - 你的 'first' 行在上面是 0
)
但你也可以保留你正在做的,然后测试 $.
:
while ( my $line = <FILE> ) {
chomp;
next unless $. > 2;
push ( @lines, $line );
last if $. > 15;
}
应该做同样的事情。
实际上您的代码几乎没有错误。它不起作用的唯一原因是您在找到范围的最后一行时调用 exit
。这意味着程序立即停止并且永远不会执行 print @lines
语句
您还无缘无故地打开了输入文件两次,但这不会造成任何问题
下面是我的写法。请注意,我使用了 autodie
编译指示,因此我不需要为任何 IO 操作显式编写错误处理程序代码
use strict;
use warnings;
use v5.14.1;
use autodie;
use constant FILE => 'info.text';
use constant FIRST_LINE => 2;
use constant LAST_LINE => 15;
open my $fh, '<', FILE;
my @lines;
while ( <$fh> ) {
push @lines, $_ if FIRST_LINE .. LAST_LINE;
last if $. == LAST_LINE;
}
print @lines;
所以我对 Perl 还很陌生,才学了 1 周。我试图只将特定范围的行读入数组。如果我在 if 语句中打印 $_,它会准确列出我想要存储到我的数组中的内容。但是将 $_ 存储到我的数组中,然后在 while 之外打印 @array 什么也没有显示。我不确定我应该做什么。我试图将它存储到一个数组中的原因是太从列中获取某些信息,因此需要一个数组来这样做。谢谢你的帮助。对你们来说可能真的很简单
use strict;
use warnings;
my $filename = 'info.text';
open my $info, $filename or die "Could not open $filename: $!";
my $first_line = 2;
my $last_line = 15;
open(FILE, $filename) or die "Could not read from $filename, program halting.";
my $count = 1;
my @lines;
while(<FILE>){
if($count > $last_line){
close FILE;
exit;
}
if($count >= $first_line){
#print $_;
push @lines, $_;
}
$count++;
}
print @lines;
Perl 实际上有一个变量,$.
,表示最近使用的文件句柄的当前行:
来自perldoc -v $.
:
HANDLE->input_line_number( EXPR )
$INPUT_LINE_NUMBER
$NR
$.
Current line number for the last filehandle accessed.
Each filehandle in Perl counts the number of lines that have been read from it. (Depending on the value of $/ , Perl's idea of what constitutes a line may not match yours.) When a line is read from a filehandle (via readline() or <> ), or when tell() or seek() is called on it, $. becomes an alias to the line counter for that filehandle.
您可以使用这个变量来大大简化您的代码:
use strict;
use warnings;
my $filename = 'info.text';
open(FILE, $filename)
or die "Could not read from $filename, program halting.";
my @lines;
while(<FILE>){
next unless $. >= 2 && $. <= 15;
push @lines, $_;
}
close FILE;
print @lines;
您可以将此代码或修改后的版本包装在接受文件句柄、起始行和结束行的子例程中,以使其更加灵活。
另外一个跟你手头的问题无关的说明,推荐你always use three argument open。
用数字表示更简单:
my @lines = (<FILE>)[1..14];
(注意 - perl
数组从零开始 - 你的 'first' 行在上面是 0
)
但你也可以保留你正在做的,然后测试 $.
:
while ( my $line = <FILE> ) {
chomp;
next unless $. > 2;
push ( @lines, $line );
last if $. > 15;
}
应该做同样的事情。
实际上您的代码几乎没有错误。它不起作用的唯一原因是您在找到范围的最后一行时调用 exit
。这意味着程序立即停止并且永远不会执行 print @lines
语句
您还无缘无故地打开了输入文件两次,但这不会造成任何问题
下面是我的写法。请注意,我使用了 autodie
编译指示,因此我不需要为任何 IO 操作显式编写错误处理程序代码
use strict;
use warnings;
use v5.14.1;
use autodie;
use constant FILE => 'info.text';
use constant FIRST_LINE => 2;
use constant LAST_LINE => 15;
open my $fh, '<', FILE;
my @lines;
while ( <$fh> ) {
push @lines, $_ if FIRST_LINE .. LAST_LINE;
last if $. == LAST_LINE;
}
print @lines;