#!/usr/bin/perl
use strict;
use warnings;
my @reqLines;
my @cleanReqLines;
my %hits;
#function to strip whitespace, which seperate fields, and replace it with commas. After substr the trailing comma
#the result is a comma seperated list
sub stripWS($)
{
my $line=shift;
$line =~ s/\s+/,/g;
$line=substr($line,0,-1);
return $line;
}
#returns a url version of the file name. substr off the .txt
sub getUrl($)
{
my $file=shift;
$file =~ s/^.*?-//;
$file =~ s/-/\//;
$file=substr($file, 0, -4);
return $file
}
sub formatDate
{
my @months=qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
my $full = shift;
#print "full:$full\n";
my @pieces=split("/", $full);
my $month=$pieces[0];
my $mindex = int($month) - 1;
my $day=$pieces[1];
my $year=$pieces[2];
# my $monthno=substr($month, 0, -2);
# print $months[$mindex]; exit;
my $monthAbr=$months[$mindex];
my $pretty=$day."/".$monthAbr."/20".$year;
print "pretty:$pretty\n";
return $pretty;
}
# sub printHits(%)
# {
# my %hitshash=shift;
# my @keys=sort(keys(%hitshash));
# foreach my $key(@keys)
# {
# print "-->$key\n";
# foreach my $line(@{$hitshash{$key}})
# {
# print " $line\n";
# }
# }
#
# }
my $dir='RequestedPages/';
opendir(my $required, $dir) or die $!;
while(my $file=readdir($required))
{
next unless (-f "$dir/$file");
my $url = &getUrl($file);
open(my $reqfile, "<", "$dir/$file") or die $!;
chomp(@reqLines=<$reqfile>);
my $year=3;
foreach my $tmpLine(@reqLines)
{
if($tmpLine =~ /#/)
{
next;
}
if(length($tmpLine) == 0)
{
next;
}
my $tmpLine=&stripWS($tmpLine);
$tmpLine.=",$url";
my @parts=split(',',$tmpLine);
if($parts[1] eq "01/01")
{
$year++;
}
# print "$parts[1]\n";
$tmpLine = join(",",@parts);
(substr($tmpLine, index($tmpLine, '/') + 3,0)) = "/0$year";
@parts=split(',',$tmpLine);
my $cleanDate=&formatDate($parts[1]);
$tmpLine = join(",",@parts);
push(@{$hits{$cleanDate}}, $tmpLine);
push(@cleanReqLines, $tmpLine);
#print "TMPLINE: $tmpLine \nFILE:$file\n";
}
}
&printHits(%hits);