$ head tst
a
b
c
d
e
a
b
c
d
e

$ perl -i -pne 'BEGIN{$a=1}$a++ if (s/a/$a a/)' tst

$ head tst
1 a
b
c
d
e
2 a
b
c
d
e


when you have a -n
it takes the whole block and puts it inside
while(<>){
#code goes here
}

what happened was that $a=1. was done every line.

BEGIN{}
goes before the while.
-a
does a @F=split

in the first line.

----
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

-- vish




On 20 February 2012 09:17, sawyer x <[email protected]> wrote:

>
> On Mon, Feb 20, 2012 at 9:33 AM, Dov Levenglick 
> <[email protected]>wrote:
>
>> Why isn't $a incrementing in this one-liner:
>> perl -ibak -pe "$a=1; $a++ if (s/   ADDR_RESET_PAIR/    $a
>> ADDR_RESET_PAIR/);" file.txt
>>
>
> -p is line based. Perhaps $a is getting initialized every time.
> If it doesn't maintain the scope, there is no way it will work.
>
>
> _______________________________________________
> Perl mailing list
> [email protected]
> http://mail.perl.org.il/mailman/listinfo/perl
>
_______________________________________________
Perl mailing list
[email protected]
http://mail.perl.org.il/mailman/listinfo/perl

Reply via email to