in reply to Non-destructive array processing
Another alternative, which I think I prefer. Best thing is you don't get "Use of uninitialized value in join or string at ..." if the array size isn't an exact multiple of the chunk size.
sub getIter (\@$;$) { my ($ref, $size, $next) = @_; $next ||= 0; return sub { $next = 0, return () unless $next <= $#$ref; my $start = $next; $next = $next+$size <= $#$ref ? $next+$size-1 : $#$ref; @$ref[ $start .. $next++ ] } } my $iter = getIter( @array, 2 ); while( my @chunk = $iter->() ) { print "Chunk: @chunk"; }
Examine what is said, not who speaks.
The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Non-destructive array processing
by dragonchild (Archbishop) on Jan 21, 2003 at 16:21 UTC | |
by BrowserUk (Patriarch) on Jan 21, 2003 at 17:28 UTC | |
|
Re: Re: Non-destructive array processing
by Juerd (Abbot) on Jan 21, 2003 at 19:30 UTC |