perl,如果第行和regex匹配,则忽略第行并移到文件中的下一行



您将如何在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)/{$1 eq 'winter' ? 'summer' : 'SUMMER'}/gie){
                print ".";
            }
        }
        print "nSeason changed in file $_";
        open FILE, ">$file";
        print FILE @lines;
        close FILE;
    }
}

只需使用下一个

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

是一种更简洁的方法

map { print "." if s/SUMMER/WINTER/g }
    grep {!/ImportantLineNotToBeChanged/} @lines;

(我想我说得对。)

只需使用下一个功能。

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
    next;
  }
  if ($line =~ s/SUMMER/WINTER/g){
    print ".";
  }
}

同样,您可以使用"last"来完成循环。例如:

for $line (@lines) {
  if ($line =~ m/ImportantLineNotToBeChanged/){
    #continue onto the next iteration of the for loop.
    #skip everything in the rest of this iteration.
    next;
  }
  if ($line =~ m/NothingImportantAFTERThisLine/){
    #break out of the for loop completely.
    #continue to code after loop
    last;
  }
  if ($line =~ s/SUMMER/WINTER/g){
    print ".";
  }
}
#code after loop

编辑:6/13 晚上7点

我拿着你的代码看了看,重写了一些东西,这就是我得到的:

sub changeSeason2 {
  my $file= $_[0];
  open (FILE,"<$file");
  @lines = <FILE>;
  close FILE;
  foreach $line (@lines) {
    if ($line =~ m/'?Don't touch this line'?/) {
         next;
       }
       if ($line =~ m/'?Or this line'?/){
         next;
       }
       if ($line =~ m/'?Or this line too'?/){
         next;
       }
       if ($line =~ m/'Or this line as well'/){
         next;
       }
       if ($line =~ s/(WINTER)/{$1 eq 'winter' ? 'summer' : 'SUMMER'}/gie){
        print ".";
       }
  }
  print "nSeason changed in file $file";
      open FILE, ">$file";
  print FILE @lines;
  close FILE;
}

希望这对一些人有所帮助。

除非我误解了你,否则你的"跳出for循环,移到下一行…[然后]再次开始循环"只是一种复杂的说法"跳过循环体进行此迭代"。

for $line (@lines) {
  unless (($line =~ m/ImportantLineNotToBeChanged/) {
    if ($line =~ s/SUMMER/WINTER/g) {
      print ".";
    }
  }
}

最新更新