http://qs321.pair.com?node_id=392839

johnnywang has asked for the wisdom of the Perl Monks concerning the following question:

Is there an one liner to do the following, which is not hard to do in more than a few lines: Given an array, and an integer, want to distribute this integer roughly evenly among this array elements, result should be a hash:
my @array=qw(a b c d e); my $num = 13; # so every element should get 2 = int(13/5), with the # remaining 3 given to the first three (or randomly). # Desired result: my %result = (a=>3, b=>3,c=>3,d=>2,e=>2);
Thanks.

My current version is something like:

my $averge = int($num/@array); my %result = map{$_=>$average} @array; for my $i(1..($num - $average*@array)){ ++$result{$array[$i-1]}; }