Slight change to Perl script converting csv file to text file. Change from minute, hour, 3 hour

Posted on 2011-09-09
Medium Priority
Last Modified: 2012-05-12
I'm using this script


use strict;
use warnings FATAL => 'all';

my $file = shift;

open(F, $file);
my @count_3 = ();
my $working_date = '';
my $working_time = '';
my $working_hour = 0;
my @avgs = ();
use Data::Dumper;
use List::Util qw(sum);

sub convert_hour($$) {
	my $hour = shift;
	my $minutes = shift;
	return 60 * $hour + $minutes;

while (<F>) {
	next unless /\S/;
	my (@data) = split(/\,/);
	next unless $data[1] =~ m|(\d+/\d+/\d+) (\d+)\:(\d\d)|;
	my $current_date = $1;
	my $current_time = "$1 $2:$3";
	my $current_hour = convert_hour($2,$3);
	next if $data[2] < 0 || $data[2] == 999;
	if ($current_date ne $working_date) {
		$working_date = $current_date;
		$working_time = $current_time;
		$working_hour = $current_hour;
		@count_3 = ($data[2]);
	if ($current_hour >= $working_hour + 180 && @count_3 >= 3) {
			[$data[0], $working_time, sum(@count_3)/@count_3],
		@count_3 = ();
		$working_time = $current_time;
		$working_hour = $current_hour;
	push(@count_3, $data[2]);
print join(
	"\n", map(
		join(" ",@{$_}),
), "\n";

Open in new window

The difference is that I now have a file format that has hourly data instead of minute day. I want three hour averages beginning at hours 0, 1, & 2 . The only two differences is that the file format gives me hourly instead of minute data (see new attached file) AND I want the data points divided by 1000 (the file gives me parts per billion but I want parts per million.) See my original post and attached file.

Question by:libertyforall2

Accepted Solution

parparov earned 2000 total points
ID: 36517082
If you want to divide by 1000, just add, at the end of line 43, after last @count_3 but before right bracket
 / 1000 

Open in new window

Your previous data file had also hourly data - all the lines were tagged by :00 time.

Author Closing Comment

ID: 36520312

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

In this post we will learn different types of Android Layout and some basics of an Android App.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

588 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question