<?xml version="1.0" encoding="windows-1252"?>
<node id="929350" title="reset of match variables" created="2011-10-03 10:29:28" updated="2011-10-03 10:29:28">
<type id="115">
perlquestion</type>
<author id="726291">
morgon</author>
<data>
<field name="doctext">
Hi&lt;p&gt;
in scripts I usually match like this:
&lt;c&gt;
my ($match) = $string =~ /$regex/;
&lt;/c&gt;
but sometimes I do one-liners like this:
&lt;c&gt;
perl -ne '/$regex/, $SUM += $1; END { print $SUM }' file.txt
&lt;/c&gt;
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.&lt;p&gt;
For curiosity's sake now:&lt;p&gt;
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?</field>
</data>
</node>
