Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: breaking an array into nearly equal parts

by nicoaimetti (Acolyte)
on Dec 15, 2005 at 01:07 UTC ( [id://516820]=note: print w/replies, xml ) Need Help??


in reply to breaking an array into nearly equal parts

Another one...
#!/usr/bin/perl -w use strict; my @A= 1..int(rand(21)); print "\@A: ",scalar @A,$/; my @parts; $parts[3-$_] = [splice @A,0, @A%$_?int(@A/$_)+1:int(@A/$_)] foreach (r +everse 1..3); #@A is empty now. print join( ' ',@$_)," : ",scalar @$_,$/ for @parts;
But I prefer the one posted by TedPride.

Replies are listed 'Best First'.
Re^2: breaking an array into nearly equal parts
by nicoaimetti (Acolyte) on Dec 15, 2005 at 04:41 UTC
    Simple improvement...
    $parts[$n-$_] = [splice @A, 0, (@A/$_)+(@A%$_&&1)] for reverse 1..$n;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-04-26 09:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found