gpt4 book ai didi

perl - 忽略文件中的行直到匹配并处理之后的行

转载 作者:行者123 更新时间:2023-12-02 07:34:23 26 4
gpt4 key购买 nike

我循环遍历文件中的行,当匹配特定行时,我想处理当前(匹配)行之后的行。我能做到:-

open my $fh, '<', "abc" or die "Cannot open!!"; 
while (my $line = <$fh>){
next if($line !~ m/Important Lines below this Line/);
last;
}
while (my $line = <$fh>){
print $line;
}

有没有更好的方法来做到这一点(代码需要成为更大的 perl 脚本的一部分)?

最佳答案

我会使用 flip-flop operator :

while(<DATA>) {
next if 1 .. /Important/;
print $_;
}
__DATA__
skip
skip
Important Lines below this Line
keep
keep

输出:

keep
keep

关于perl - 忽略文件中的行直到匹配并处理之后的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18328978/

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