Perl中的文件处理:从特定行读取文件 [英] File Handling in Perl: Reading a file from a specific line

查看:163
本文介绍了Perl中的文件处理:从特定行读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在Perl中开始从一个特定的行号读取文件。每当我们在perl中读取一个文件时,我们都会执行(<$ 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆