#!/usr/bin/perl use strict; use warnings; use Data::Dump 'pp'; use Time::Piece; use Date::Calc qw(:all); my $DIR = 'c:/temp'; my @VEH = qw(car bike); my $hr_count = get_count($DIR); #pp $hr_count; report($hr_count,'report.txt'); sub get_count { my $dir = shift; my %count = (); my $match = join '|',@VEH; my %WK_JAN1=(); # for my $file (glob "$dir/*.txt"){ while (my $file = ){ if ($file =~ /(20\d{6})_($match)/){ my $date = $1; my $veh = $2; #open IN,'<',$file or die "Could not open $file : $!"; #my @lines = ; #my $count = scalar @lines; #close IN; my $count = rand(100); my $t = Time::Piece->strptime($date,"%Y%m%d"); my $y = $t->year; # do this only once unless (exists $WK_JAN1{$y}){ $WK_JAN1{$y} = Time::Piece->strptime($y.'0101',"%Y%m%d")->week } my $wk = $t->week; if ($t->mon == 1 && $wk > 5){ $wk = 1; } elsif ($WK_JAN1{$y} > 1) { $wk += 1; } my $week = sprintf "%4d-%2d",$t->year,$wk; $count{$week}{$date}{'wday'} = $t->wdayname; # Mon Tue Wed etc $count{$week}{$date}{$veh} = $count; $count{$week}{$date}{'total'} += $count; $count{$week}{'total'}{'wday'} = ''; $count{$week}{'total'}{$veh} += $count; $count{$week}{'total'}{'total'} += $count; } } return \%count; } sub report { my ($count,$filename) = @_; #open OUT,'>',$filename # or die "Could not open $filename : $!"; my $fmt_s = '%8s %8s'.(' %10s' x @VEH)." %10s \n"; my $fmt_d = '%8s %8s'.(' %10d' x @VEH)." %10d \n"; for my $wk (sort keys %$count){ my ($get_year, $get_week) = split /-/,$wk; my ($year2, $month2, $day2) = Monday_of_Week($get_week, $get_year); my $format_date = "$year2-$month2-$day2"; my $get_time = Time::Piece->strptime($format_date, '%Y-%m-%d'); print "\n".$get_time->fullmonth." ".$get_time->year." Week $get_week\n------------\n"; #print "\nWeek $wk\n------------\n"; printf $fmt_s,('Date','Day',@VEH,'Total'); for my $date (sort keys %{$count->{$wk}}){ my $rec = $count->{$wk}{$date}; my @values = map{$rec->{$_} || 0 }(@VEH,'total'); printf $fmt_d,$date,$rec->{wday},@values } } #close OUT } __DATA__ 20151231_car.txt 20160102_car.txt 20160102_bike.txt 20160104_car.txt 20160104_bike.txt 20160208_car.txt 20160208_bike.txt 20160308_car.txt 20160308_bike.txt 20160309_car.txt 20160309_bike.txt 20160314_car.txt 20160314_bike.txt 20160315_car.txt 20160315_bike.txt 20160316_car.txt 20160316_bike.txt 20161221_car.txt 20161231_bike.txt 20180101_car.txt 20180101_bike.txt