Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Re^4: Keeping a Count in foreach

by YuckFoo (Abbot)
on Jun 18, 2002 at 17:40 UTC ( [id://175437]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Keeping a Count in foreach
in thread Keeping a Count in foreach

Arrrgh! I give up. Where's the friggin line counter? I understand all the pieces (that I see), but cannot figure out where the counter is being stored. There must be some dark implicitness going on underneath all this explicitness. Or am I just a dullard?

YuckFoo

Replies are listed 'Best First'.
Re^6: Keeping a Count in foreach
by particle (Vicar) on Jun 18, 2002 at 18:17 UTC
    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 Operators

    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.

    therefore:

    ( ( $INPUT_LINE_NUMBER or ONE ) .. ( $INPUT_LINE_NUMBER and ZERO ) )
    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}

    try:

    perl -le "print( ($.||1..undef)-1 )for 1..4"
    for a (somewhat) less obscure but equivalent obfuscation.

    i hope that helps a little. mmm... dark magic.

    ~Particle *accelerates*

      Your use of
      ($. || 1 .. undef) -1
      totally baffles me. This must be an obscure effect. I read this as a scalar use of the range operator with start and stop conditions but you say that it's in list context instead.

      I can reduce this to:

      ((undef or 1) .. undef) - 1
      but no more.

      Can you shed more light on what anonymous variable is keeping count?

      metadoktor

      "The doktor is in."

        Can you shed more light on what anonymous variable is keeping count?

        The flip-flop range operator remembers its state, so in a way, each flip-flop has it's own anonymous variable. And it is in scalar context.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://175437]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-03-29 13:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found