Perl doesn't reset these variables if the regex doesn't match which is why you always have to test whether the regex was true before using $1, $2, etc. From <a href="http://perldoc.perl.org/perlre.html">http://perldoc.perl.org/perlre.html</a>:<br>
<br><div><span class="Apple-style-span" style="font-family: 'Helvetica Neue', Arial, Helvetica, Geneva, sans-serif; font-size: 13px; color: rgb(81, 81, 81); "><b>NOTE</b>: Failed matches in Perl do not reset the match variables, which makes it easier to write code that tests for a series of more specific cases and remembers the best match.</span></div>
<div><font class="Apple-style-span" color="#515151" face="'Helvetica Neue', Arial, Helvetica, Geneva, sans-serif"><br></font></div><div><font class="Apple-style-span" color="#515151" face="'Helvetica Neue', Arial, Helvetica, Geneva, sans-serif"><font class="Apple-style-span" color="#000000">In your case the range operator as a whole remains true even while the LHS is false which is why you have to rewrite it the way you did.</font></font></div>
<div><font class="Apple-style-span" color="#515151" face="'Helvetica Neue', Arial, Helvetica, Geneva, sans-serif"><font class="Apple-style-span" color="#000000"><br></font></font></div><div><font class="Apple-style-span" color="#515151" face="'Helvetica Neue', Arial, Helvetica, Geneva, sans-serif"><font class="Apple-style-span" color="#000000">Garth<br>
</font></font><br><div class="gmail_quote">2010/9/16 yary <span dir="ltr"><<a href="http://not.com">not.com</a>@<a href="http://gmail.com">gmail.com</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Hi all,<br><br>I'm going through logs seeing if a change improved a particular SQL statements execution time. The part I'm interested looks like this:<br><br> insert into foo..bar_440_5678_D<br> select<br> a.pd,<br>
<a href="http://a.tg" target="_blank">a.tg</a>,<br> ....<br> Elapsed time: 0m27.485s<br><br>I'm interested in the "5678" and the time it took. Seems like the flip-flip ".." is perfect for a quick hack to extract that info:<br>
<br>perl -ne 'if(/insert into foo\.\.bar_440_(\d+)/../time: (.+)/&&$1){print "$1\n"}' SQL.log<br><br>The thing is, it prints "5678" many times, until it gets to the "Elapsed time" portion. I would expect that the lines between the start and the end, which don't match, to clear $1.<br>
<br>I ended up with a longer one-liner to extract the info I was looking for-<br><br>perl -ne 'if(((/insert into foo\.\.bar_440_(\d+)/ and $a=$1)..(/time: (.+)/ and $b=$1))&&$b){print "$a took $b\n";$b=0}' SQL.log<br>
<br>Now I'm curious why I couldn't rely on $1 being undefined for the intermediate lines. Anyone have a good explanation?<br><font color="#888888"><br>-y<br><br><br>
</font><br>_______________________________________________<br>
SanFrancisco-pm mailing list<br>
<a href="mailto:SanFrancisco-pm@pm.org">SanFrancisco-pm@pm.org</a><br>
<a href="http://mail.pm.org/mailman/listinfo/sanfrancisco-pm" target="_blank">http://mail.pm.org/mailman/listinfo/sanfrancisco-pm</a><br>
<br></blockquote></div><br></div>