perlquestion
morgon
Hi<p>
in scripts I usually match like this:
<c>
my ($match) = $string =~ /$regex/;
</c>
but sometimes I do one-liners like this:
<c>
perl -ne '/$regex/, $SUM += $1; END { print $SUM }' file.txt
</c>
In this context I was just bitten by the (documented) behaviour that a failed match does not reset the match variables which in my case lead to a wrong sum as on empty lines where the regex did not match the previous match was (again) added to the sum.<p>
For curiosity's sake now:<p>
Assuming I did not want to introduce new variables but use $1 etc: How would I reset these variables to avoid the wrong sum as both "undef $1" as "$1=undef" does not work?