I am writing a Perl script which parses an Excel file. The aim of this script is to count for each cell value in column 1, the number of values it has in column 2.
Per example an Excel file that looks like this :
12 abc
12 abc
12 efg
12 efg
13 hij
13 hij
13 klm
My script would return:
For cell value 12 I have :
2 values "abc", 2 values "efg" and for cell value 13 i have : 2 values "hij" and 1 value "klm".
My script would look something like this (I took this example from the perl doc) :
use Spreadsheet::XLSX;
my $excel = Spreadsheet::XLSX -> new ('Book1.xlsx');
foreach my $sheet (@{$excel -> {Worksheet}}) {
printf("Sheet: %s\n", $sheet->{Name});
$sheet -> {MaxRow} ||= $sheet -> {MinRow};
foreach my $row ($sheet -> {MinRow} .. $sheet -> {MaxRow}) {
$sheet -> {MaxCol} ||= $sheet -> {MinCol};
foreach my $col ($sheet -> {MinCol} .. $sheet -> {MaxCol}) {
my $cell = $sheet -> {Cells} [$row] [$col];
if ($cell) {
#here I should count the cell values
}
print $cell;
}
}
}
I have no idea how to do this since I’ve never used perl before in my life, and I can’t find examples online that match what I want exactly. Any help would be much much appreciated.
Thanks
Perhaps the following commented script will help:
Output:
You can do the following to show the values associated with key ’13’:
Output: