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

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

  •  23-07-2023
  •  | 
  •  

Question

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
}
Était-ce utile?

La solution

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

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

Autres conseils

You mean?

if ($line =~ s/^REF:  //){
    print $line."\n";
}
else {
   print "Line not touched\n";
   }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top