in reply to Even column spreading of data

I read this as wanting the data spread in columns each with an equal number of rows (except the last column which has the remainder) - like columns of words on a magazine page. If this is correct, I offer the following ;
use strict; use warnings; my @data = 1 .. 99; my $cols = 6 ; my $items = $#data + 1; my $rows = int ($items / $cols); $rows += 1 if ($items % $cols); for my $r (1..$rows){ for my $c (1..$cols){ my $i = ($r - 1) + ($c - 1) * $rows; print $data[$i]."\t"; } print "\n"; }

Replies are listed 'Best First'.
Re: Re: Even column spreading of data
by Kage (Scribe) on Dec 22, 2002 at 23:12 UTC
    Your code, as simple as it may be, worked as one of the best, except for one fatal flaw. Often times, when I would do different variations of items and columns, which would return maybe 1 or 0 as the final column's amount of items, it would throw a wrench into the entire assembly. Another instance was when I would try things such as FamousLongAgo did, such as doing erroristic combinations of like 99 rows of 2 items, or 0 rows of 99 items. Not saying I need these precautions, but you never know where being safe may help in appliance.
    “A script is what you give the actors. A program is what you give the audience.” ~ Larry Wall