gpt4 book ai didi

Perl:读取文件时跳过第 N 行

转载 作者:行者123 更新时间:2023-12-04 22:13:58 25 4
gpt4 key购买 nike

在解析文本文件时如何跳过第 9 行?

这是我得到的

use strict; use warnings;
open(my $fh, '<', 'file.txt') or die $!;
my $skip = 1;
while (<$fh>){
$_=~ s/\r//;
chomp;
next if ($skip eq 9)
$skip++;
}

不确定这是否有效,但我确信这样做更有说服力。

最佳答案

您可以使用 $. :

use strict; use warnings;
open(my $fh, '<', 'file.txt') or die $!;
while (<$fh>){
next if $. == 9;
$_=~ s/\r//;
chomp;
# process line
}

也可以使用 $fh->input_line_number()

关于Perl:读取文件时跳过第 N 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15793367/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com