I am trying to perform a simple check to see if a string contains a “%” but whenever I execute my code it will evaluate the if statement as false no matter what is in the string. My code looks like this:
if ($end_time =~ m{%%}) {
($percentage) = $end_time =~ m/=([^%%]*)%%/g;
$percentage = sprintf("%s%%", $percentage);
$end_time = "";
}
elsif ($duration =~ m/(overdue)/) {
$percentage = "$end_time $duration";
$end_time = "";
}
else {
$percentage = "100%";
}
and the expected values of $end_time, substitute whatever numeric values you like.
"5%" or "==30%" or "+3m:26s overdue" or "13:48:40"
So if $end_time contains “overdue” or a “%” percentage will be 100% and $end_time will be whatever was in there before the check. And I do understand why I’m getting the results I’m getting, just not the reason my if statements are always evaluating as false.
String literals and regex literals escape with
\, not by doubling, and%does not need to be escaped in either.sprintftakes a string with doubled%to indicate%, but that’s not related to building strings and regex patterns.