I have program that prints the highest interval every 5 minutes. I have previously used this same format with another similar array, but I am getting a few errors this time:
- Use of uninitialized value $min in division …
- Use of uninitialized value $yr in sprintf …
-
Use of uninitialized value in sprintf at …
use strict; use warnings; use Text::CSV_XS; my %interval; my %month; @month{qw/ jan feb mar apr may jun jul aug sep oct nov dec /} = '01' .. '12'; foreach my $line (@lastArray){ $line =~ s/$/,/; my ($dow, $mon, $day, $hr, $min, $sec, $yr, $amt) = split /[:,]/, $line; my $key = sprintf "%4d-%02d-%02d %02d:%02d", $yr, $month{lc $mon}, $day, $hr, int($min / 5) * 5; if (exists $interval{$key}) { if ($interval{$key}{amt} > $amt) { $interval{$key}{amt} = $amt; $interval{$key}{data} = [split ",", $line]; } } else { # first time in this 5 minute interval $interval{$key}{amt} = $amt; $interval{$key}{data} = [split ",", $line]; } } my $csv = Text::CSV_XS->new ({ binary => 1 }) or die "Cannot use CSV: ".Text::CSV_XS->error_diag (); $csv->eol("\n"); # Initialize CSV files open my $fh, ">", 'log_5min.csv' or die $!; open my $FILE, ">", 'last_5min.stat' or die $!; # Print Max Busy for all intervals print $fh "DayofWeek,Month,Day,Time,Year,rdy,bsy,rd,wr,ka,log,dns,cls\n"; for my $time (sort keys %interval) { $csv->print($fh, $interval{$time}{data}); }
The array @lastArray contains the following:
Wed,Jun,13,01:00:29,2012,777,23,0,15,6,0,0,2,15,0,0
Wed,Jun,13,01:01:29,2012,782,18,0,14,3,0,0,1,14,0,0
Wed,Jun,13,01:02:29,2012,787,13,0,10,3,0,0,0,10,0,0
Wed,Jun,13,01:03:29,2012,782,18,0,15,2,0,0,1,15,0,0
Wed,Jun,13,01:04:29,2012,779,21,0,12,9,0,0,0,12,0,0
Wed,Jun,13,01:05:30,2012,777,23,0,18,5,0,0,0,18,0,0
Wed,Jun,13,01:06:30,2012,783,17,0,10,4,0,0,3,10,0,0
Wed,Jun,13,01:07:30,2012,786,14,0,6,5,0,0,3,6,0,0
Wed,Jun,13,01:08:30,2012,789,11,0,6,5,0,0,0,6,0,0
How can I fix the errors? Thank you for your help.
“Use of uninitialized value $min in division”: One of the values in @lastArray has fewer than 3 “:” or “,”.
“Use of uninitialized value $yr in sprintf”: One of the values in @lastArray has fewer than 6 “:” or “,”
etc.
PS – Get rid of
$line =~ s/$/,/;. It doesn’t do anything useful.