Another solution
sub spread {
return ($_[0] % $_[1])?join("-", ((int($_[0] / ($_[1] - 1))) x ($_
+[1]-1), ($_[0] % ($_[1] - 1)))):join("-", (int($_[0] / $_[1])) x $_[1
+]);
}
print spread(19,4); #test first branch of ?:
print spread(25,5); #test second branch of ?:
Update: I read FamousLongAgo's reply, and thanks for testing my code.
However I don't think there is any problem, although in the 22/7 case, that 4 in the last column is greater than 3's in the rest columns. This really depends on how you define "evenly", and what "remainder goes to last column" means.
My understanding is that other than the last column, other columns should be exactly the same; Whatever left goes to last column. It is never said anywhere in his post, that the last column cannot be greater than the rest.
However the value of the last column must be less than $col - 1 (number of columns - 1), if it is greater than or equals to "number of columns - 1", then it is really not even, and you really should redistribute.
As for whether the code is reable, I don't think it is too difficult to understand, as obviously you understood, and tried to reformat it, but this is more perlier ;-)