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.


In reply to Re (tilly) 1: DBI output in 3 columns by tilly
in thread DBI output in 3 columns by Donzo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.