<div dir="ltr">Oh, I didn't notice the $a=1.<br><br>This is what it looked before the BEGIN{}:<br>BEGIN { $^I = ""; }<br>LINE: while (defined($_ = <ARGV>)) {<br> ++$a if s/a/$a a/;<br>}<br>continue {<br>
die "-p destination: $!\n" unless print $_;<br>}<br><br>This is what it looked after:<br>BEGIN { $^I = ""; }<br>sub BEGIN {<br> $a = 1;<br>}<br>LINE: while (defined($_ = <ARGV>)) {<br> ++$a if s/a/$a a/;<br>
}<br>continue {<br> die "-p destination: $!\n" unless print $_;<br>}<br><br>And, in case you're wondering, you can write BEGIN as "sub BEGIN {}" but it's not advised to do so. Apparently when you define it, it *is* what the perl interpreter does. Odd..<br>
<br><div class="gmail_quote">On Mon, Feb 20, 2012 at 12:14 PM, Avishalom Shalit <span dir="ltr"><<a href="mailto:avishalom@gmail.com">avishalom@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
$ head tst<br>a<br>b<br>c<br>d<br>e<br>a<br>b<br>c<br>d<br>e<br><br>$ perl -i -pne 'BEGIN{$a=1}$a++ if (s/a/$a a/)' tst <br><br>$ head tst<br>1 a<br>b<br>c<br>d<br>e<br>2 a<br>b<br>c<br>d<br>e<br><br><br>when you have a -n<br>
it takes the whole block and puts it inside <br>while(<>){<br>#code goes here<br>}<br><br>what happened was that $a=1. was done every line. <br><br>BEGIN{}<br>goes before the while. <br>-a<br>does a @F=split<br><br>
in the first line. <br><br>----<br>p.s. remember that $a is magic , sort{$a<=>$b} works in strict because $a is already declared. so you may want to use another name. $c is good <br><br clear="all">-- vish<br><br><br>
<br><br><div class="gmail_quote"><div><div class="h5">On 20 February 2012 09:17, sawyer x <span dir="ltr"><<a href="mailto:xsawyerx@gmail.com" target="_blank">xsawyerx@gmail.com</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div><div class="h5">
<div dir="ltr"><br><div class="gmail_quote"><div>On Mon, Feb 20, 2012 at 9:33 AM, Dov Levenglick <span dir="ltr"><<a href="mailto:dov.levenglick@gmail.com" target="_blank">dov.levenglick@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Why isn't $a incrementing in this one-liner:<br>
perl -ibak -pe "$a=1; $a++ if (s/ ADDR_RESET_PAIR/ $a<br>
ADDR_RESET_PAIR/);" file.txt<br></blockquote></div><div><br>-p is line based. Perhaps $a is getting initialized every time.<br>If it doesn't maintain the scope, there is no way it will work.<br><br></div></div></div>
<br></div></div><div class="im">_______________________________________________<br>
Perl mailing list<br>
<a href="mailto:Perl@perl.org.il" target="_blank">Perl@perl.org.il</a><br>
<a href="http://mail.perl.org.il/mailman/listinfo/perl" target="_blank">http://mail.perl.org.il/mailman/listinfo/perl</a><br></div></blockquote></div><br>
<br>_______________________________________________<br>
Perl mailing list<br>
<a href="mailto:Perl@perl.org.il">Perl@perl.org.il</a><br>
<a href="http://mail.perl.org.il/mailman/listinfo/perl" target="_blank">http://mail.perl.org.il/mailman/listinfo/perl</a><br></blockquote></div><br></div>