Data::Table Perl Module
Programming at High Level
[ Download | Documentation | History | CPAN | Support ]
News
The latest release is 1.54 (Feb. 10, 2008).
Relase 1.52 adds a new method Data::Table::fromFile(file_name).
It read automatically guess which OS generated the file, whether the first row is a column header, and whether the file is comma, tab, or colon-separated. It relies on the following new methods
fromFileGuessOS, fromFileGetFileLine, fromFileIsHeader, and fromFileGuessDelimiter to figure out either to call Data::Table::fromCSV or Data::Table::fromTSV with the correct arguments.
Please read document for details.
Since release 1.41, two very cool methods, group & pivot, have been added to the package.
Example
#!/usr/bin/perl
use Data::Table;
use CGI;
use DBI;
$q = new CGI;
print $q->header;
$dbh = DBI->connect("DBI:mysql:test", 'test', '');
$t = Data::Table::fromSQL($dbh,
"select * from test.aa order by Entry");
$t->rename('Aminoacid', 'Amino acid');
$t->rename('Grams', 'Grams per 100g sol.');
$t->rename('Temp', 'Temp (C)');
$t->colMap('Amino acid',
sub { "<a href='http://search.yahoo.com/bin/search?p=$_'>$_</a>"
});table::html
print "<b>table::html</b>";
print $t->html;
print "<br><b>table::html2</b>";
print $t->html2;
| Entry | Amino acid | Solvent | Grams per 100g sol. | Temp (C) | Ref_No |
|---|---|---|---|---|---|
| 1 | Alanine | 0.00 | 0.01 | 25.00 | 1 |
| 2 | L-aspartate | 0.00 | 0.00 | 25.00 | 2 |
| 3 | L-glutamate | 0.00 | 0.01 | 44.93 | 2 |
| 4 | Glycine | 0.00 | 0.00 | 25.00 | 1 |
table::html2
| Entry | 1 | 2 | 3 | 4 |
|---|---|---|---|---|
| Amino acid | Alanine | L-aspartate | L-glutamate | Glycine |
| Solvent | 0.00 | 0.00 | 0.00 | 0.00 |
| Grams per 100g sol. | 0.01 | 0.00 | 0.01 | 0.00 |
| Temp (C) | 25.00 | 25.00 | 44.93 | 25.00 |
| Ref_No | 1 | 2 | 2 | 1 |
New methods: group & pivot (Version 1.41, to be available)
$t = new Data::Table(
[
['Tom', 'male', 'IT', 65000],
['John', 'male', 'IT', 75000],
['Peter', 'male', 'HR', 85000],
['Mary', 'female', 'HR', 80000],
['Nancy', 'female', 'IT', 55000],
['Jack', 'male', 'IT', 88000],
['Susan', 'female', 'HR', 92000]
],
['Name', 'Sex', 'Department', 'Salary'], 0);
sub average {
my @data = @_;
my ($sum, $n) = (0, 0);
foreach $x (@data) {
next unless $x;
$sum += $x; $n++;
}
return ($n>0)?$sum/$n:undef;
}
$t2 = $t->group(["Department","Sex"],["Name", "Salary"],
[sub {scalar @_}, \&average],["Nof Employee", "Average Salary"]);
print $t2->html;
| Sex | Department | Nof Employee | Average Salary |
|---|---|---|---|
| male | IT | 3 | 76000 |
| male | HR | 1 | 85000 |
| female | HR | 2 | 86000 |
| female | IT | 1 | 55000 |
| Department | female | male |
|---|---|---|
| IT | 55000 | 76000 |
| HR | 86000 | 85000 |