Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Round robin processing

by trwww (Priest)
on Sep 09, 2019 at 16:42 UTC ( [id://11105891] : note . print w/replies, xml ) Need Help??


in reply to Round robin processing

Your solution is fine. Heres how I'd probably write it:

$ cat 11105885.pl use warnings; use strict; use Data::Dumper; my $bucket_count = 4; my @array = qw(1 2 3 4 5 6 7 8 9 10 11 12); my $buckets = []; for ( my $counter = 0; $counter < @array; $counter++ ) { my $element = $array[ $counter ]; my $bucket = $buckets->[ $counter % $bucket_count ] ||= []; push @$bucket, $element; } print Data::Dumper->Dump([$buckets], [qw(buckets)]);

The result:

$ perl 11105885.pl $buckets = [ [ '1', '5', '9' ], [ '2', '6', '10' ], [ '3', '7', '11' ], [ '4', '8', '12' ] ];