Skip to content

Instantly share code, notes, and snippets.

@hikiko hikiko/report-validator.pl
Last active Apr 11, 2019

Embed
What would you like to do?
parses a csv with info on projects and hours and counts the hours that correspond to each project
use strict;
use warnings;
use Getopt::Long;
my $report;
my $help;
GetOptions("r=s", => \$report,
"h" => \$help);
if($help) {
print qq["Help:
-r <path to the Downloaded csv>, input report.
-h , print this help and exit.\n];
exit;
}
die "You must specify a report.\n" unless $report;
die "Invalid filename: $report. Exiting. $!\n" unless (-f $report);
my $hours;
my $total_hours = 0;
my %categories;
my $line_count = 0;
open(my $fh, '<', $report) or die "Couldn't open file $report. $!\n";
while(!eof($fh)) {
defined(my $line = readline $fh) or die "Couldn't open $report. $!\n";
if (!$line_count) {
if(!($line =~ m/.*Hours.*\,.*Description.*/)) {
close($fh);
die "Invalid report file\n";
}
}
if ($line =~ m/([0-9]+)\:([0-9]+).*\,.*\[(.*)\]/) {
$hours = $1 + $2 / 60.0;
$total_hours += $hours;
my $category = $3;
if ($categories{$category}) {
$categories{$category} += $hours;
} else {
$categories{$category} = $hours;
}
}
$line_count++;
}
close($fh) or die $!;
foreach my $key (keys %categories) {
print qq[Project $key: $categories{$key}\n];
}
print qq[Total hours: $total_hours\n];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.