for(my $i=1; $i < scalar @files; $i++) { #everything same as before until we get to reading the files if(-e $files[$i]){ open IN, "gunzip -c $files[$i]|" or die "Can't open $files[$i] for input: $!"; open OUT, "> $files[$i]_out.txt" or die "Can't open $files[$i]_out.txt for output: $!"; my $firstline = 1; my @dt = undef; # date and time my @date1 = undef; my @date2 = undef; if($firstline==1){ chomp(my $line = ); my @header = split(/,/, $line); @dt= $header[0] =~ /(\d+)/g; @date1 = ($dt[0], $dt[1], $dt[2], $dt[3], $dt[4], $dt[5]); @date2 = ($dt[6], $dt[7], $dt[8], $dt[9], $dt[10], $dt[11]); $firstline = 0; } else { for 1..1 } print OUT "Date\tTime\tDay\tmg\n"; my $count = 0; my $avg_count = 0; while(){ #this is where it changes, instead of doing the same thing 12 times I say if the line number (-1 due to header) modulos 12 is 0 then print the average and set it back to 0 my ($y, $mo, $d, $h, $m, $s) = Add_Delta_DHMS(@date1, 0, 0, $count, 0); chomp($_); my @line = split(/,/, $_); $avg_count += $line[0]; if((($.)-1)%12 ==0){ printf OUT qq(%d-%02d-%02d %02d:%02d:%02d), $y, $mo, $d, $h, $m, $s; print OUT "\t" . Day_of_Week($y, $mo, $d); print OUT "\t" . ($avg_count/12) . "\n"; $count +=1; $avg_count =0; } } close IN; close OUT } }