I've been working on a project to take data from csv files, re-format it, and display it as charts. I'm working for a company over the summer and when I leave, I would like my code to be readable/editable by whoever takes over in the future.
This code reads in two files, formats them, and creates two output files. It takes in one command line argument as a filepath to the source folder for the data. The formatting just counts the number of occurrences of a word and increments a corresponding value in a 2D array.
Would you understand what this is doing if I hadn't given an explanation? Should I change anything about it to make it more readable?
#!/usr/bin/perl
use strict;
use warnings;
my $src = $ARGV[0];
my %files = (
'LM' => [$src . '\raw\casesByCategory_LM.csv', $src . '\casesByCategoryLM.csv'],
'TM' => [$src . '\raw\casesByCategory_TM.csv', $src . '\casesByCategoryTM.csv'],
);
foreach my $file (keys %files) {
my @categories = (
['RMA Request', 0],
['Product Info Request', 0],
['Dealer Locator (New or Resupply)', 0],
['Order Request', 0],
['Order Status', 0],
['Wireless Activation/Deactivation', 0],
['Pricing', 0],
['Account Setup/Registration', 0],
['Other', 0],
);
my $total = 0;
open (DATA, $files{$file}[0]) or die $!;
# Remove header
<DATA>;
my $match = 0;
while (my $line = <DATA>) {
# Remove \n character
chomp $line;
# Search through list of categories to find a match
if ($line =~ /^"(.+)"$/) {
foreach my $cat (@categories) {
if ($1 eq $cat->[0]) {
$cat->[1]++;
$match = 1;
}
}
}
# Add case to 'Other' if no match was found
if (not $match) {
$categories[8]->[1]++;
} else {
$match = 0;
}
$total++;
}
close (DATA);
# Write organized data to new CSV file for chart
open (OUTPUT,'>', $files{$file}[1]) or die $!;
print OUTPUT "Total: $total\n";
foreach my $cat (@categories) {
print OUTPUT "$cat->[0],$cat->[1]\n";
}
close (OUTPUT);
}