Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Round robin processing

by jcb (Parson)
on Sep 09, 2019 at 16:18 UTC ( [id://11105888]=note: print w/replies, xml ) Need Help??


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

    Thanks for those ideas. That's interesting, I thought about modulo but wasn't sure how to use it , because remainder is often 0 : e.g. : 8%1 , 8%2, 8%4 all have a remainder of 0.

      The remainder being 0 is not really a problem and is needed for the solutions presented thus far, because all of them are using arrays to store the bins instead of using a hash. Arrays in Perl are indexed using numbers starting at 0, so it "just fits" and also mean that the bins are always in a known order instead of the random order that your initial code produces.

      those with a remainder of 0 will go to bin 0, i.e. the first slot in the bins array.

Re^2: Round robin processing
by rsFalse (Chaplain) on Sep 10, 2019 at 09:36 UTC
    I tried a bit similar to 'slice.pl':

    perl -wle 'use Data::Dumper; my @buckets; my $buckets = 4; @a = 1 .. 1 +4; push @buckets, [ grep defined, @a[ map { $_ * $buckets } 0 .. @a / + $buckets ] ] xor shift @a for 1 .. 1 + @a / $buckets; print Dumper( +@buckets )'
    output:
    Useless use of logical xor in void context at -e line 1. $VAR1 = [ 1, 5, 9, 13 ]; $VAR2 = [ 2, 6, 10, 14 ]; $VAR3 = [ 3, 7, 11 ]; $VAR4 = [ 4, 8, 12 ];
    upd.Slightly changed a name of variable $bucket to $buckets.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11105888]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-26 03:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found