|
Data::Pivot - Perl module to pivot a table |
Data::Pivot - Perl module to pivot a table
use Data::Pivot;
@newtable = pivot( table => \@table,
headings => \@headings,
pivot_column => $pivot_col_no,
layout => 'vertical',
row_sum => 'Sum',
row_titles => 1,
format => '%5.2f',
)
With Data::Pivot you can pivot a table like this:
Some Fix Columns Pivot_Col Num_Values
aaa bbb ccc 01 12.20 aaa bbb ccc 02 134.50 aaa bbb ccc 03 1.25 xxx yyy zzz 02 22.22 xxx yyy zzz 03 111.11
Will be converted to:
Some Fix Columns 01 02 03 Sum
aaa bbb ccc 12.20 134.50 1.25 147.95 yyy xxx zzz 0.00 22.22 111.11 133.33
The table can contain several columns of Num_Values, which will get into rows, if the layout is 'horizontal', like this:
Some Fix Columns Pivot_Col Num_Val_1 Num_Val_2 Num_Val_3
aaa bbb ccc 01 12.20 1.40 5.90 aaa bbb ccc 02 134.50 12.00 12.30 aaa bbb ccc 03 1.25 30.00 123.45 xxx yyy zzz 02 22.22 7.80 8.88 xxx yyy zzz 03 111.11 100.00 42.00
Will be converted to:
Some Fix Columns 01 02 03 Sum
aaa bbb ccc Num_Val_1 12.20 134.50 1.25 147.95
Num_Val_2 1.40 12.00 30.00 43.40
Num_Val_3 5.90 12.30 123.45 141.65
xxx yyy zzz Num_Val_1 0.00 22.22 111.11 133.33
Num_Val_2 0.00 7.80 100.00 107.80
Num_Val_3 0.00 8.88 42.00 50.88
Data::Pivot has only one function which does all the work.
pivot()
pivot receives several named parameters:
In the last example above:
@table = ( [ 'aaa', 'bbb', 'ccc', '01', 12.2, 1.4, 5.9 ],
[ 'aaa', 'bbb', 'ccc', '02', 134.5, 12, 12.3 ],
[ 'aaa', 'bbb', 'ccc', '03', 1.25, 30, 123.45 ],
[ 'xxx', 'yyy', 'zzz', '02', 22.22, 7.8, 8.88 ],
[ 'xxx', 'yyy', 'zzz', '03', 111.11, 100, 42 ]
);
In the last example above:
@headings = ('Some', 'Fix', 'Columns', 'Pivot_Col', 'Num_Val_1', 'Num_Val_2', 'Num_Val_3');
In the last example above:
$no_of_col = 3;
The full function call for the above example is:
@newtable = pivot( table => \@table,
headings => \@headings,
pivot_column => $pivot_col_no,
row_sum => 'Sum',
row_titles => 1,
format => '%5.2f',
);
Bernd Dulfer <bdulfer@cpan.org>
Graham TerMarsch
|
Data::Pivot - Perl module to pivot a table |