in reply to Re: Even column spreading of data
in thread Even column spreading of data

Same code ( what is this, Lisp? ), reformatted to be more readable and do a sanity check for the zero-columns case. Note that it doesn't seem to do exactly what the questioner asked - see the 22/7 case:
#!/usr/bin/perl -w use strict; while (<DATA> ) { my ( $items, $cols ) = split; print "Spread for $items, $cols = ", spread( $items, $cols ), "\n"; } sub spread { my ($a, $cols) = @_; $a ||= 0; return unless $cols > 0; # avoid nasty surprises return $a if $cols == 1; #trivial case if ( $a % $cols) { return join '-', ( (int( $a / ($cols - 1) )) x ( $cols - 1) +, ( $a % ($cols - 1))); } else { return join '-', (int ( $a / $cols)) x $cols; } } __DATA__ 19 3 20 5 22 7 2 47 0 9 899 0
Results:
Spread for 19, 3 = 9-9-1 Spread for 20, 5 = 4-4-4-4-4 Spread for 22, 7 = 3-3-3-3-3-3-4 Spread for 2, 47 = 0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0 +-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-2 Spread for 0, 9 = 0-0-0-0-0-0-0-0-0 Spread for 899, 0 =