in reply to Automatic Loop Counter not in perl

you could only use it in the innermost loop. Any loop would reset it to zero when starting

Even better would be for it to be automatically localized, just as $_ is in foreach loops.

A word spoken in Mind will reach its own level, in the objective world, by its own weight

Replies are listed 'Best First'.
Re^2: Automatic Loop Counter not in perl
by holli (Abbot) on Aug 21, 2007 at 15:59 UTC
    Why not just make it a stack? We can easily index the last element(s) by using negative offsets, so:
    for my $outer ( 0..1 ) { for my $inner ( 5..6 ) { print $LOOPCOUNT[-1], "+", $LOOPCOUNT[-2], "\n"; } }


    holli, /regexed monk/
      Why not just make it a stack?

      What do you think local is doing?

      One of the cool things about localizable variables is that when you take a reference to one, you're getting a reference to that instance of it in its internal stack. So, if you need to reach back to values in outer scopes, you can take references to the variable at those levels.

      use strict; use warnings; our $LC = 'o'; my $lc0 = \$LC; for ( local $LC = 0; $LC < 3; $LC++ ) { my $lc1 = \$LC; for ( local $LC = 0; $LC < 3; $LC++ ) { print "lc0=$$lc0, lc1=$$lc1, LC=$LC, \n"; } }
      A word spoken in Mind will reach its own level, in the objective world, by its own weight
        I am aware what local does, i've used it in various occasions. Having a "real" stack (array), would save me from having to do the extra typing of creating an explicit reference. And I could even know the loop count of whatever outer code calls MY code.


        holli, /regexed monk/