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;