in reply to for loops, closures
Maybe this should be fixed with scoping $i so it can't be reached by the last block?
update:This is a bit silly, but this is the way I solved it when I had the same problem ("my $o" in second last block):
/jeorgenmy ( @ref_list, $code_ref, $i ); #pass one #for SCALAR LIST for $i (1..8) { push( @ref_list, sub { print " \$i == $i\n" } ); } print "for SCALAR LIST\n"; while ( $code_ref = pop @ref_list ) { &$code_ref; } #pass 2 #for INIT ; TEST ; INCREMENT for ( $i = 1; $i <= 8; $i++ ) { my $o; $o=$i; push( @ref_list, sub { print " \$i == $o\n" } ); } print "for (;;)\n"; while ( $code_ref = pop @ref_list ) { &$code_ref; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
RE: Re: for loops
by Aighearach (Initiate) on Aug 20, 2000 at 15:58 UTC | |
by tilly (Archbishop) on Aug 21, 2000 at 00:41 UTC | |
by BlaisePascal (Monk) on Aug 21, 2000 at 00:55 UTC | |
by tilly (Archbishop) on Aug 21, 2000 at 01:11 UTC | |
by Aighearach (Initiate) on Aug 21, 2000 at 05:14 UTC |