Perl中的文件处理:从特定行读取文件 [英] File Handling in Perl: Reading a file from a specific line
问题描述
(<$ fileHandler>)
,这使得perl解释器从头开始读取文件。如果我不想阅读这些文章,该怎么办? 解决方案 $ start_line
,
my $ start_line = 10;
变量)。
while(< $ fileHandler>){
next除非$。 == $ start_line .. undef;
code
$ b
范围运算符..
还提供以下内容简写:
$ b
如果标量..的任何一个操作数是一个常量表达式,那么该操作数在相等时被认为是真的(<$ (
$。
blockquote>
因此,上面可以简化为:
while(< $ fileHandler>){
next除非10 .. undef;
#..
}
Is there no way in perl to start reading a file from a specific line number. Whenever we read a file in perl we do
while(<$fileHandler>)
which makes the perl interpreter to read the file from beginning. What to do if I don't want to read these lines?解决方案You can skip lines from the beginning, and start processing with
$start_line
,my $start_line = 10; while(<$fileHandler>) { next unless $. == $start_line .. undef; # .. }
The
range operator ..
also provides the following shorthand:If either operand of scalar ".." is a constant expression, that operand is considered true if it is equal (
==
) to the current input line number (the$.
variable).Therefore the above can be reduced to:
while(<$fileHandler>) { next unless 10 .. undef; # .. }
这篇关于Perl中的文件处理:从特定行读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!