in reply to Seek one liner for distributing an integer

$result{$array[$n++]} = int($num / ($#array + 1)) + ($num % ($#array + 1) > $n) while ($n <= $#array + 1);

This produces the results you want in one run through @array - 5 steps - rather than $num steps. One of the other examples given above also attempts to do this, but I can't get the code to work, so here's mine.

NOTE: $n is created global here, overwriting any other global or local $n in use. I couldn't do a my statement without using a second line.

Replies are listed 'Best First'.
Re^2: Seek one liner for distributing an integer
by ikegami (Patriarch) on Sep 22, 2004 at 15:22 UTC
    Found a small bug when doing benchmarks. Remove the "+ 1" from while ($n <= $#array + 1);.