in reply to Re: Re^4: Keeping a Count in foreach
in thread Keeping a Count in foreach
Arrrgh!yeah, i get that ;-)
I0 wrote some nasty looking line noise. i found it frighteningly logical. that code shows I0 knows perlop. i took the time to translate this to English, but it doesn't make it much clearer. why? because the secret is in the range operator.
from perlop:
Range Operatorstherefore:Binary ".." is the range operator, which is really two different operators depending on the context. In list context, it returns an array of values counting (up by ones) from the left value to the right value. If the left value is greater than the right value then it returns the empty array. The range operator is useful for writing foreach (1..10) loops and for doing slice operations on arrays.
is the range operator returning in list context. it's operating on some number >=1, to 0. it returns the empty array -- with length of the left number. subtract one to make it zero-based indexing (and to put it in scalar context,) and you've got your counter. {evil grin}( ( $INPUT_LINE_NUMBER or ONE ) .. ( $INPUT_LINE_NUMBER and ZERO ) )
try:
for a (somewhat) less obscure but equivalent obfuscation.perl -le "print( ($.||1..undef)-1 )for 1..4"
i hope that helps a little. mmm... dark magic.
~Particle *accelerates*
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^7: Keeping a Count in foreach
by metadoktor (Hermit) on Jun 21, 2004 at 21:46 UTC | |
by runrig (Abbot) on Jun 21, 2004 at 22:22 UTC |