in reply to How to make references more like aliases?
To batch through an array I'd collect indexes so there is no doubt but that I am dealing with the actual elements in the loop body.
Like:
Original post followsmy $i; my @ar = 0 .. 10; ($get_idx_grp, $fin_idx_grp) = mk_idx_funcs( 4, \$i, \@ar ); for ( $i = 0; $i < @ar; ++$i) { my @idx = &$get_idx_grp; $ar[ $idx[0] ] = "YYY"; &$fin_idx_grp; } sub mk_idx_funcs { # XXX bad name my ( $grp_size, $iter, $array ) = @_; my ( @group, $group_end ); my $first_call = 1; return ( sub { if ($first_call) { $first_call = 0; $group_end = $$iter + $grp_size; } if ( $$iter < $group_end and $$iter < @$array ) { push @group, $$iter; no warnings; next unless @group==$grp_size or $$iter==@$array-1; } return @group; }, sub { $group_end += @group; @group = (); return; } ); }
I would be interested in the aliasing most likely to avoid changing the identifiers in a loop body. Your code does not do this, so I do not understand what you are trying to achieve.
This seems simpler:
#!/usr/bin/perl use strict; use warnings; my @array = 0 .. 30; my $interval = make_interval(); for my $val ( @array ) { &$interval( 4); $val = "--"; }; local $, = " "; print @array, $/; sub make_interval { my $ctr = 0; my $start = 1; return sub { if ( $start) { $start = 0; return } $ctr++; if ( $ctr == $_[0]) { $ctr = 0 } else { no warnings; next } } }
|
|---|