News
Perl Data::Table Cookbook is available! (ver 1.09, last modified Mar 22, 2016)
[Download Cookbook]
[Download Cookbook]
The latest Data::Table release is 1.74 (Mar 22, 2016).
We release Data::Table::Excel (0.3) to read/write Excel .xls/.xlsx into/from Data::Table.
We release Data::Table::Excel (0.3) to read/write Excel .xls/.xlsx into/from Data::Table.
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 |