I have a Perl data structure like the following:
%hash = (
table_id => {
column_1_header =>
[
column_value_1,
column_value_2,
column_value_3
],
column_2_header =>
[
column_value_4,
column_value_5,
column_value_6,
column_value_7,
],
},
);
My goal is to produce a series of tables with each of the columns as outlined in the structure. For example:
<table id="table_id">
<tbody>
<tr>
<td>column_1_header</td>
<td>column_2_header</td>
</tr>
<tr>
<td>column_value_1</td>
<td>column_value_4</td>
</tr>
<tr>
<td>column_value_2</td>
<td>column_value_5</td>
</tr>
<tr>
<td>column_value_3</td>
<td>column_value_6</td>
</tr>
<tr>
<td></td>
<td>column_value_7</td>
</tr>
</tbody>
</table>
I am using Perl Dancer and Template Toolkit. What are some options to easily a table like this?
Change the data so it's grouped by record instead of by column.
use Algorithm::Loops qw( MapCarU );
for my $table_id (keys(%hash)) {
my $table = $hash{$table_id};
my @headers = keys(%$table);
my @recs = ( \@headers, MapCarU(sub { [ @_ ] }, @{$table}{@headers} ) );
$hash{$table_id} = \@recs;
}
The above changes
%hash = (
table_id => {
col_1_header => [ val_1, val_2, val_3 ],
col_2_header => [ val_4, val_5, val_6, val_7 ],
},
);
to
%hash = (
table_id => [
[ col_1_header, col_2_header ],
[ val_1, val_4 ],
[ val_2, val_5 ],
[ val_3, val_6 ],
[ undef, val_7 ],
],
);
Note the the data structure does not contain enough information to recreate the original column order.