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

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

  •  23-07-2023
  •  | 
  •  

Pregunta

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
}
¿Fue útil?

Solución

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

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

Otros consejos

You mean?

if ($line =~ s/^REF:  //){
    print $line."\n";
}
else {
   print "Line not touched\n";
   }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top