#!/usr/bin/perl use strict; use warnings; use Data::Dump 'pp'; use Data::Dumper; # Create Time::Piece New Object my $t = Time::Piece->new(); report(); exit; sub report { my %count_cars; my %count_bike; my %count_cars_wkly; my %count_bike_wkly; my $dir = '/data'; my $todays_date = $t->mdy("/"); # Set days ago my $past = $t - (7 * ONE_DAY); my $just_today = $t->strftime('%Y%m%d'); opendir( DIR, $dir ) || die "Unable to open directory - $!\n"; my @files = grep /\.txt/, readdir( DIR ); closedir( DIR ); foreach my $file (@files) { # Open all files if ($file=~/(20\d{2}\d{2}\d{2})_cars/) { my $a_file = $1; my $file_date = Time::Piece->strptime($a_file, '%Y%m%d'); # Weekly if ( $file_date > $past) { open( FA, "$dir/$file" ) || die "Unable to open $file - $!\n"; while( ) { $count_cars_wkly{$_}++ if ( defined( $_ ) ); } close( FA ); } # Daily if ( $a_file) { open( FA, "$dir/$file" ) || die "Unable to open $file - $!\n"; while( ) { $count_cars{$_}++ if ( defined( $_ ) ); } close( FA ); } }elsif($file=~/(20\d{2}\d{2}\d{2})_bike/) { my $h_file = $1; my $file_date = Time::Piece->strptime($h_file, '%Y%m%d'); # Weekly if ( $file_date > $past) { open( FH, "$dir/$file" ) || die "Unable to open $file - $!\n"; while( ) { $count_bike_wkly{$_}++ if ( defined( $_ ) ); } close( FH ); } # Daily if ( $just_today eq $h_file) { open( FH, "$dir/$file" ) || die "Unable to open $file - $!\n"; while( ) { $count_bike{$_}++ if ( defined( $_ ) ); } close( FH ); } } } # end open all files print "\n Daily number of cars processed: " . scalar keys %count_cars; print "\n Daily number of bike processed: " . scalar keys %count_bike; my $cars_total = scalar keys %count_cars; my $bike_total = scalar keys %count_bike; my $total = $cars_total + $bike_total; print "\n\n Total Daily Number processed as $todays_date: $total\n\n"; print "\n Weekly number of cars processed: " . scalar keys %count_cars_wkly; print "\n Weekly number of bike processed: " . scalar keys %count_bike_wkly; my $cars_wkly_total = scalar keys %count_cars_wkly; my $bike_wkly_total = scalar keys %count_bike_wkly; my $wkly_total = $cars_wkly_total + $bike_wkly_total; print "\n\n Total Daily Number processed as $todays_date: $wkly_total\n\n"; } =code SAMPLE Files FILE CARS: 20160316_car.txt CARa CARb VEs WEV CARE5 TYR5 FILE BIKE: 20160316_bike.txt KIR OERl wejg WEDFH WERF 20160315_car.txt ASWWWa ASDCARb TITVEs CCDWEV CARE5 XTYR5 20160315_bike.txt QAKIR VBBOERl OIIwejg QWWEDFH QWWWWERF =cut