in reply to Re: Automatic Loop Counter not in perl
in thread Automatic Loop Counter not in perl

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/

Replies are listed 'Best First'.
Re^3: Automatic Loop Counter not in perl
by jdporter (Paladin) on Aug 21, 2007 at 19:56 UTC
    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/