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