Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Re^2: Keeping a Count in foreach

by BUU (Prior)
on Jun 17, 2002 at 20:36 UTC ( [id://175179]=note: print w/replies, xml ) Need Help??


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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re^4: Keeping a Count in foreach
by particle (Vicar) on Jun 17, 2002 at 22:36 UTC
    yeah, nasty code. i should probably give an englishEnglish version as contrast.

    use English; sub ONE() {1} sub ZERO() {0} for my $each (@strings) { print( ( ( $INPUT_LINE_NUMBER or ONE ) .. ( $INPUT_LINE_NUMBER and ZERO ) ) - ONE, ' ', $each, $INPUT_RECORD_SEPARATOR, ); }

    ~Particle *accelerates*

      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

        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*

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-03-28 21:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found