in reply to Round robin processing
Well, at least writing these was enjoyable. Here are two solutions, both slightly adjusted to have data that does not evenly fit the number of bins.
modulo.pl:
#!/usr/bin/perl use strict; use warnings; my @array = 1 .. 14; use constant BINS => 4; my @bins = (); for my $i (0 .. $#array) { print "i = $i:\t$array[$i]\n"; push @{$bins[$i % BINS]}, $array[$i]; } use Data::Dumper; print Dumper \@bins;
sample output:
i = 0: 1 i = 1: 2 i = 2: 3 i = 3: 4 i = 4: 5 i = 5: 6 i = 6: 7 i = 7: 8 i = 8: 9 i = 9: 10 i = 10: 11 i = 11: 12 i = 12: 13 i = 13: 14 $VAR1 = [ [ 1, 5, 9, 13 ], [ 2, 6, 10, 14 ], [ 3, 7, 11 ], [ 4, 8, 12 ] ];
slice.pl
#!/usr/bin/perl use strict; use warnings; my @array = 1 .. 14; use constant BINS => 4; my @bins = (); for my $i (0 .. (BINS - 1)) { push @bins, [@array[grep {defined $array[$_]} map {BINS * $_ + $i} 0 .. (@array / BINS)]]; } use Data::Dumper; print Dumper \@bins;
sample output:
$VAR1 = [ [ 1, 5, 9, 13 ], [ 2, 6, 10, 14 ], [ 3, 7, 11 ], [ 4, 8, 12 ] ];
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Round robin processing
by llarochelle (Beadle) on Sep 09, 2019 at 17:17 UTC | |
by jcb (Parson) on Sep 09, 2019 at 17:26 UTC | |
by bliako (Abbot) on Sep 10, 2019 at 15:31 UTC | |
|
Re^2: Round robin processing
by rsFalse (Chaplain) on Sep 10, 2019 at 09:36 UTC |