I've retrieved some values from a DB, and want to display them in a table. The data is sorted alphabetically in an array, and I want the results to appear as follows (two columns):
A F
B G
C H
D I
E J
I want each value to be in its own cell. What's a good programmatic way to build a table, since it kinda wants me to finish each ROW in its entirety before moving on? (Wheras I want to build the <td> for "A", then for "B", since I'm iterating through my loop... it would be hard to build the <td> for "A", then for "F", then for "B"... etc.)
In other words, it would be trivial to contruct the following:
A B
C D
E F
G H
I J
But what's a cool way to build a column-centric table, rather than row-centric?
Hope I've described it well (enough).
Perl Table Column Coding [webmasterworld.com]
Andreas
(I'm assuming that 'shift' is expensive because it's moving stuff around...)
Just curious.
My implementation:
@array = ( 1, 2, 3, 4, 5 );
$count = @array;
$half = ( $count + 1 ) / 2;
@col_1 = @$section_ref[ 0 .. $half - 1 ];
@col_2 = @$section_ref[ $half .. $count - 1 ];
for ( $i = 0; $i < @col_2; $i++ ) {
# <tr><td> $col_1[$i] </td><td> $col_2[$i] </td></tr>
}
if ( @col_1 > @col_2 ) {
# <tr><td> $col_1[-1] </td></tr>
}