Riley wrote:
> 
> Hello, All!
> 
> I believe I understand ton's alternative solution...
> 
> -p0 s/\.(?=.{$&}(x.{$&})+o)/*/sgwhile$_=reverse,998877e3=~/./g
> 
> (At first I was confused as to why 998877e3 was needed
> as opposed to 998877e2, but I suppose since ton is
> reversing the string in the while condition, rather
> than in the while block, he needed one more character
> in order to flip the string one final time. Is there
> any other purpose for having e3?)

Try:

perl -MO=Deparse reversi.pl

and you'll see that 998877e3 is converted to 998877000 internally.
It's the odd number of 0's that are needed to get the string
reversed back to the desired direction, because of where the
reverse function is. 998877e9 also works :)

> 
> ...But I'm having trouble understanding the winning
> solution:
> 
> -p0 $_=reverse,s/\.(?=(.{$&}x)+.{$&}o)/*/swhile$^C+=9870=~/./g
> 
> I looked up the $^C predefined variable and found:
> The current value of the flag associated with the -c
> switch.
> 
> I don't really understand what this means, but I
> feel that even if I did, I wouldn't be able to figure
> out how incrementing this variable (or apparently
> decrementing it, cf. mtve's solution) makes the while
> loop run more than 4 times. (I have a feeling,
> however, that the loop is running many more than
> 9 times as in ton's alternate solution above, and
> on first glance, I would have declared that we had
> created an infinite loop.)
> 
> Many thanks,
> Riley o0lit3

$^C is a golf trick first found (I think) by mtve.

The perl special variable $^C is apparently kept internally
in a single 8-bit byte. It's value is restricted to between
-128 and 127 (inclusive), and it starts with an initial value of 0.
It wraps from 127 to -128 when incrementing, and -128 to 127
when decrementing. It can be used (as it is here) as a counter
to 256.

Try:

perl -le 'print $^C while ++$^C'

to get a better idea of its behavior.

$^C can be avoided by using $^H as a counter, as in 

-p0 $_=reverse,s/\.(?=(.{$&}x)+.{$&}o)/*/swhile$^H-=9870=~/./g

$^H does not have the special properties that $^C does, but $^H does
have an initial value of 256.


I hope this helps!

-- 
Rick Klement (tybalt89)

Reply via email to