in reply to How to make references more like aliases?
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to make references more like aliases?
by Limbic~Region (Chancellor) on Sep 28, 2004 at 12:47 UTC | |
by Prior Nacre V (Hermit) on Sep 29, 2004 at 05:57 UTC | |
by Limbic~Region (Chancellor) on Sep 29, 2004 at 13:29 UTC | |
by Prior Nacre V (Hermit) on Sep 30, 2004 at 03:14 UTC |