Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
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' ] ];

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-25 13:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found