in reply to DBI output in 3 columns

Here is a more advanced answer. It assumes familiarity with how to use the list-oriented nature of Perl, how to use map, and how to access arrays of arrays as described in perlref.

I would do the rearranging of data in a function. Suppose that you can create an array @cells of cells above. I assume that you want it sorted by column and then row. Here is a possible rearrangement functions you could use:

# Takes a number of columns then a list. Reformats the list into # an array of arrays, with the original elements sorted down by # columns, and any missing items missing off of the last column sub reformat_major_minor { my $dim = shift; my $row_size = int((@_+$dim - 1)/$dim); my @ret; foreach my $i (0..$#_) { $ret[$i%$row_size][$i/$row_size] = $_[$i]; } return @ret; }
And then you can produce your output with:
foreach my $row (reformat_major_minor(3, @cells)) { print "<tr>\n", (map " <td>$_</td>\n", @$row), "</tr>\n"; }
Should you dislike taking the missing elements on the last column and want to take it on the last row instead, you can use the following reformat function instead:
# Takes a number of columns then a list. Reformats the list into # an array of arrays, with the original elements sorted down by # columns, and any missing items missing off of the last column. sub reformat_major_minor { my $dim = shift; my $row_size = int(@_/$dim); my @ret; push @ret, [splice @_, 0, $row_size] while @_; return swap_dims(@ret); } # Takes an array of arrays and swaps rows for columns. sub swap_dims { my @ret; foreach my $row_ind (0..$#_) { my $row = $_[$row_ind]; foreach my $col_ind (0..$#$row) { $ret[$col_ind][$row_ind] = $row->[$col_ind]; } } return @ret; }

UPDATE
Oops. I put the wrong reformatting function up there in the first slot. Now fixed.