Is there a way to combine a substitution regex and match test in one line?

StackOverflow https://stackoverflow.com/questions/23671520

  •  23-07-2023
  •  | 
  •  

문제

The answer is probably obvious, but I'm wondering if there's a shorter way to write this:

if ($line =~ m/^REF:  /){
    $line =~ s/^REF:  //;
    # do something else
}
도움이 되었습니까?

해결책

s/// returns the number of substitutions made. An equivalent of your code would be:

if ($line =~ s/^REF:  //) {
    # do something else
}

다른 팁

You mean?

if ($line =~ s/^REF:  //){
    print $line."\n";
}
else {
   print "Line not touched\n";
   }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top