Also have a look at
Text::Table.
As for capturing the data, I think just a HoH will suffice. Your matrix presentation suggests that for each query and subject only one percent value is possible. In pseudo code:
my $matrix;
my %seen_queries;
for each row in the excel file:
my ($query, $subject, $percent) = values from the excel row
$matrix->{$subject}->{$query} = $percent;
$seen_queries{$query} = 1;
end for
And to display the matrix:
use Text::Table;
my @cols = keys %seen_queries;
my $tb = Text::Table->new(@cols);
for my $subject (keys %$matrix) {
$tb->load( [ @{$matrix->{$subject}}{@cols} ] );
}
print $tb;
This code demonstrates:
- the use of a hash to keep track of seen identifiers (%seen_queries)
- using a HoH to store a 2-D matrix of data
- hash slices to conveniently extract multiple values from a hash at once
Also, when writing this code I had to decide if I wanted to store the data as
$matrix->{$subject}->{$query} or
$matrix->{$query}->{$subject}. The latter organization would still work but would have made the table display code slightly more complex. Here's how it would have worked out:
use Text::Table;
my @cols = keys %$matrix;
my $tb = Text::Table->new(@cols);
for my $subject (keys %seen_subjects) {
my @vals;
for my $query {@cols) {
push(@vals, $matrix->{$query}->{$subject});
}
$tb->load( [ @vals ] );
}
The hash
%seen_subjects would be constructed in the reading phase just like
%seen_queries but with
$subject as the hash parameter.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.