Name : perl-Text-CSV-Pivot
| |
Version : 0.09
| Vendor : obs://build_opensuse_org/devel:languages:perl
|
Release : lp154.1.1
| Date : 2023-01-27 17:59:45
|
Group : Development/Libraries/Perl
| Source RPM : perl-Text-CSV-Pivot-0.09-lp154.1.1.src.rpm
|
Size : 0.04 MB
| |
Packager : https://www_suse_com/
| |
Summary : Transform CSV file into Pivot Table format
|
Description :
Recently I was asked to prepare pivot table using csv file at work. Having done that using quick and dirty perl script, I decided to clean up and make it generic so that others can also benefit.
Below is sample data, I used for prototype as source csv file.
+----------------+-----------+--------+--------+ | Student | Subject | Result | Year | +----------------+-----------+--------+--------+ | Smith, John | Music | 7.0 | Year 1 | | Smith, John | Maths | 4.0 | Year 1 | | Smith, John | History | 9.0 | Year 1 | | Smith, John | Language | 7.0 | Year 1 | | Smith, John | Geography | 9.0 | Year 1 | | Gabriel, Peter | Music | 2.0 | Year 1 | | Gabriel, Peter | Maths | 10.0 | Year 1 | | Gabriel, Peter | History | 7.0 | Year 1 | | Gabriel, Peter | Language | 4.0 | Year 1 | | Gabriel, Peter | Geography | 10.0 | Year 1 | +----------------+-----------+--------+--------+
I aim to get something like this below.
+----------------+--------+-----------+---------+----------+-------+-------+ | Student | Year | Geography | History | Language | Maths | Music | +----------------+--------+-----------+---------+----------+-------+-------+ | Gabriel, Peter | Year 1 | 10.0 | 7.0 | 4.0 | 10.0 | 2.0 | | Smith, John | Year 1 | 9.0 | 9.0 | 7.0 | 4.0 | 7.0 | +----------------+--------+-----------+---------+----------+-------+-------+
With the help of Text::CSV::Pivot, I came up with the following solution.
use strict; use warnings; use Text::CSV::Pivot;
Text::CSV::Pivot->new({ input_file => \'sample.csv\', col_key_idx => 0, col_name_idx => 1, col_value_idx => 2 })->transform;
After executing the above code, I got the expected result in \'sample.pivot.csv\'.
|
RPM found in directory: /packages/linux-pbone/ftp5.gwdg.de/pub/opensuse/repositories/devel:/languages:/perl:/CPAN-T/15.4/noarch |