perl if line匹配正则表达式,忽略行并移动到文件中的下一行

perl if line匹配正则表达式,忽略行并移动到文件中的下一行,第1张

概述你会如何在perl中执行以下 *** 作: for $line (@lines) { if ($line =~ m/ImportantLineNotToBeChanged/){ #break out of the for loop, move onto the next line of the file being processed #start the loop 你会如何在perl中执行以下 *** 作:

for $line (@lines) {    if ($line =~ m/importantlineNottoBeChanged/){        #break out of the for loop,move onto the next line of the file being processed        #start the loop again    }    if ($line =~ s/SUMMER/WINTER/g){        print ".";    }}

更新以显示更多代码,这是我正在尝试做的事情:

sub ChangeSeason(){    if (-f and /.log?/) {        $file = $_;        open file,$file;        @lines = <file>;        close file;        for $line (@lines) {            if ($line =~ m/'?Don't touch this line'?/) {                last;            }            if ($line =~ m/'?Or this line'?/){                last;            }            if ($line =~ m/'?Or this line too'?/){                last;            }            if ($line +~ m/'?Or this line as well'?/){                last;            }            if ($line =~ s/(WINTER)/{ eq 'winter' ? 'summer' : 'SUMMER'}/gIE){                print ".";            }        }        print "\nSeason changed in file $_";        open file,">$file";        print file @lines;        close file;    }}
解决方法 只需使用 next

for my $line (@lines) {    next if ($line =~ m/importantlineNottoBeChanged/);    if ($line =~ s/SUMMER/WINTER/g){        print ".";    }}
总结

以上是内存溢出为你收集整理的perl if line匹配正则表达式,忽略行并移动到文件中的下一行全部内容,希望文章能够帮你解决perl if line匹配正则表达式,忽略行并移动到文件中的下一行所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1268578.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存