perl if line matches regex, ignore line and move onto next line in file

China☆狼群 提交于 2019-12-13 12:08:12

问题


How would you do the following in 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 ".";
    }
}

Updated to show more code, this is what I'm trying to do:

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;
    }
}

回答1:


Just use next

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



回答2:


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

A more concise method is

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

(I think I got that right.)




回答3:


Just use the next feature.

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 ".";
  }
}

Similarly, you can use "last" to finish the loop. For example:

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

Edit: 7pm on 6/13

I took your code and looked at it, rewrote some things and this is what I got:

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;
}

Hope this helps some.




回答4:


Unless I'm misunderstanding you, your "break out of the for loop, move onto the next line... [and] start the loop again" is just a complex way of saying "skip the loop body for this iteration".

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


来源:https://stackoverflow.com/questions/6308497/perl-if-line-matches-regex-ignore-line-and-move-onto-next-line-in-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!