Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Seek one liner for distributing an integer

by TedPride (Priest)
on Sep 22, 2004 at 07:32 UTC ( [id://392862]=note: print w/replies, xml ) Need Help??


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);.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-25 12:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found