I didn't really have a problem with the FUGLY line; however, I did think the closure was doing more than necessary and I generally don't like passing whole arrays, so here's yet another variation on this theme.
I have noted what appears to be a bug (but it may be the intended behaviour).
use strict; use warnings; my @array = 0 .. 30; my $rc_next = by_groups_of( 3, [ \(@array) ] ); while ( my $ra_group = $rc_next->() ) { last unless @$ra_group; ${$ra_group->[1]} = 'foo'; } print "$_\n" for @array; sub by_groups_of { my ($by, $ra_list) = @_; $by ||= 0; $ra_list ||= []; my $rc_return = sub { [] }; if ($by && $by !~ /\D/ && @$ra_list) { my $pos = 0; my $done; $rc_return = sub { return [] if $done; my $start = $pos; my $stop = $pos + $by - 1 > $#{$ra_list} ? $#{$ra_list} : +$pos + $by - 1; #$pos += $by - 1; # ?? bug ?? - changed to "$pos += $by; +" $pos += $by; $done = 1 if $stop == $#{$ra_list}; return [ @{$ra_list}[$start .. $stop] ]; }; } return $rc_return; }
Regards,
PN5
In reply to Re: How to make references more like aliases?
by Prior Nacre V
in thread How to make references more like aliases?
by Limbic~Region
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |