in reply to Fair schedule allocation?

Take your comment, and put it into data. Derive everything from it. I just happen to like AoH's, as it's easy to extend it out more.

#/usr/bin/perl use strict; use warnings; use Readonly; my @volunteers = ( { name => "Ben", chunks => 5 }, { name => "Scott", chunks => 4 }, { name => "Jim", chunks => 1 }, ); my $current = 2192; Readonly my $CHUNKSIZE => 200; use List::Util qw(sum); my $total_volunteer_chunks = sum map { $_->{chunks} } @volunteers; sub volunteer_for { my $n = shift() % $total_volunteer_chunks; for my $v (@volunteers) { $n -= $v->{chunks}; if ($n < 0) { return $v; } } } for my $chunk (0..98) { my $end = $current + $CHUNKSIZE; my $victim = volunteer_for($chunk)->{name}; print "$current-$end\t\t$victim\n"; $current = $end + 1; }

Update: Fixed the code. It works now.

Replies are listed 'Best First'.
Re^2: Fair schedule allocation?
by oko1 (Deacon) on Jan 05, 2010 at 03:23 UTC

    I'm afraid I couldn't get it to work: after fixing the 'Readonly my ...' line and adding the missing curly brace at the end of the sub, it only printed one iteration. :\ I do see the approach you're taking, though - thanks for the idea!


    --
    "Language shapes the way we think, and determines what we can think about."
    -- B. L. Whorf