CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Subtract millisecond time from file a from file b for each row

 



Penguin
Novice


Apr 10, 2011, 10:50 AM

Post #1 of 8 (1121 views)
Subtract millisecond time from file a from file b for each row Can't Post

I have 2 files one contains time string  values in the following format 09:00.000 and another file that has time string  values in the same format

I am interested in calculating the millisecond time difference between each of the two files

e.g the time in the first row of file a minus the tim in the first row of file b and so on until the end of each file. Both files contain the same number of time entries.

I thought  that I would

 -read each file into a separate array
- loop through via foreach and subtract array_a[0] from array_b[0] and so on.

Problem is I don't know how to do it

Heres my code

Code
#!/usr/bin/perl 

$ltimefile="linetime.txt";
$stimefile="sendtime.txt";
open(TZ1, $stimefile);
@stimefinal=<TZ1>;
close(TZ1);
open(TZ2, $ltimefile);
@limefinal=<TZ2>;
close(TZ2);

foreach(@ltimefile, @stimefile){

$ltimefile - $stimefile
my @components = split /[:\.]/, shift;
return (($components[0] * 60 + $components[1]) * 60 + $components[
+2]) * 1000 + $components[3];
}

}

Thx



#timestamps look like this:

07:18:00.416
17:51:41.580
07:18:10.983
17:51:41.773
07:18:11.866
17:51:41.799
07:18:12.079
17:51:41.805
07:18:13.642
17:51:41.853
07:18:13.642
17:51:41.853
07:18:13.647
17:51:41.853
07:18:13.658
17:51:41.854
07:18:13.667
17:51:41.855
07:18:14.731
17:51:41.890
07:18:14.738
17:51:41.890
07:18:14.877
17:51:41.896
07:18:14.877

#
#

Please help!


BillKSmith
Veteran

Apr 10, 2011, 12:13 PM

Post #2 of 8 (1118 views)
Re: [Penguin2020] Subtract millisecond time from file a from file b for each row [In reply to] Can't Post

Read and process one line at a time.

Write a function to do all the format conversions.

Always use strict and warnings.

Use the three argument form of open

use lexical file-handles.


Code
use strict; 
use warnings;
my $ltimefile = "linetime.txt";
my $stimefile = "sendtime.txt";
open my $TZ1, '<', $stimefile or die "unable to open $stimefile: $!";
open my $TZ2, '<', $ltimefile or die "unable to open $ltimefile: $!";
while (1) {
my $stimefinal = <$TZ1>;
last if !defined $stimefinal;
my $ms1 = min_sec2ms($stimefinal);
my $ltimefinal = <$TZ2>;
last if !defined $ltimefinal;
my $ms2 = min_sec2ms($ltimefinal);
print $ms2 - $ms1, "\n";
}
close $TZ2;
close $TZ1;

sub min_sec2ms {
my ($min_sec) = @_;
chomp $min_sec;
my ($min, $sec) = split ':', $min_sec;
return 1000 * (60*$min + $sec);
}

Good Luck,
Bill


Penguin
Novice


Apr 11, 2011, 2:26 AM

Post #3 of 8 (1099 views)
Re: [BillKSmith] Subtract millisecond time from file a from file b for each row [In reply to] Can't Post

Hi thanks

I ran thru the code but it doesnt take into account seconds or milliseconds.

If you see a timestamp it looks like this

09:00:01.902

Therefore does the function need to be rewritten to take into account the milliseconds .

so e.g
{code}
sub min_sec2ms {
my ($min_sec) = @_;
print $min_sec;
chomp $min_sec;
my ($min, $sec) = split '/:\./', $min_sec;
return 1000 * (60* $min + $sec);
}
{code}


Penguin
Novice


Apr 11, 2011, 3:47 AM

Post #4 of 8 (1097 views)
Re: [Penguin2020] Subtract millisecond time from file a from file b for each row [In reply to] Can't Post

sub min_sec2ms {
my ($min_sec) = @_;

chomp $min_sec;

my @components =split /[:\.]/,$min_sec;
return(($components[0]*60+$components[1])*60+$components[+2])*1000+$components[3];

}


BillKSmith
Veteran

Apr 11, 2011, 6:02 AM

Post #5 of 8 (1090 views)
Re: [Penguin2020] Subtract millisecond time from file a from file b for each row [In reply to] Can't Post

There is no need to separate the decimal part of seconds.


Code
sub hr_min_sec2ms { 
my ($hr_min_sec) = @_;
chomp $hr_min_sec;
my ($hr, $min, $sec) = split /:/, $hr_min_sec;
return 1000 * ( 60 * ( 60 * ( $hr ) + $min ) + $sec );
}



Notes:

My original code was based on the time format in the first line of your original post.

New code, including new symbol names, is based on the revised time format.

Parentheses around $hr are not needed, but I believe they clarify the expression.
Good Luck,
Bill


Penguin
Novice


Apr 11, 2011, 6:10 AM

Post #6 of 8 (1088 views)
Re: [BillKSmith] Subtract millisecond time from file a from file b for each row [In reply to] Can't Post

Many thanks!


Penguin
Novice


Apr 11, 2011, 1:19 PM

Post #7 of 8 (1078 views)
Re: [BillKSmith] Subtract millisecond time from file a from file b for each row [In reply to] Can't Post

For some reason all calculations are completed except for the last one. After looking at the output it looks like the last values in linetime.txt and sendtime.txt arent used in the calculation

is there something that says a while loop will return all elements in an array apart from the final one?


BillKSmith
Veteran

Apr 11, 2011, 3:36 PM

Post #8 of 8 (1076 views)
Re: [Penguin2020] Subtract millisecond time from file a from file b for each row [In reply to] Can't Post

There are many ways to code a while loop incorrectly. The loop I posted works for good data, but it does not check for errors.

It does not tolerate blank lines or comments in either data file, not even at the end.

Both data files must contain exactly the same number of lines.

The last line of each file should end in a newline.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives