Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re^5: Time matching YYYY-MM-DD HH:MM:SS.SSS

by dave_the_m (Prior)
on Jul 26, 2012 at 19:44 UTC ( #983921=note: print w/replies, xml ) Need Help??

in reply to Re^4: Time matching YYYY-MM-DD HH:MM:SS.SSS
in thread Time matching YYYY-MM-DD HH:MM:SS.SSS

So: is what you want to do to count the number of (non-epoch) lines in the earlier file that are before the first date in the second file?

Note that unless the first timestamp in the later file exactly matches one of the timestamps in the earlier file, your code will keep counting through the rest of the file.

Use the 3-arg form with lexical filehandles:

open my $fh, '<', 'newfile.txt' or die "Can't open newfile.txt: $!\n";
If use warnings was in scope where you reference @Array0[0], you would have seen a warning (not that it affects the correct execution of your code).


Replies are listed 'Best First'.
Re^6: Time matching YYYY-MM-DD HH:MM:SS.SSS
by PRyanRay (Novice) on Jul 26, 2012 at 19:54 UTC
    That is exactly correct. The timestamps will eventually match up. Essentially, one of the files may be a subset of the other. Each line has data that may be different for the same times. I need to compare that data. That part of the code is done. So, yes, I can count on the timestamps matching up once I get rid of the first n lines of the earlier file. What is the easiest (prettiest I guess) way to do this? Thanks a lot.
      You probably want something like the following (not heavily tested):
      use warnings; use strict; @ARGV == 2 or die; my @files = @ARGV; my $date_qr = qr{^(\d{4}/\d\d/\d\d \d\d:\d\d:\d\d\.\d{3}) }; my @dates; for my $i (0,1) { open my $fh, '<', $files[$i] or die "Can't open $files[$i]: $!\n"; while (<$fh>) { next if /Epoch/; chomp; m/$date_qr/ or die "$files[$i]:$.: invalid line: $_\n"; $dates[$i] = $1; last; } } print "earliest=@dates\n"; my $i = 0; if ($dates[0] ne $dates[1]) { my $earliest = $dates[0] lt $dates[1] ? 0 : 1; open my $fh, '<', $files[$earliest] or die "Can't open $files[$earliest]: $!\n"; while (<$fh>) { next if /Epoch/; chomp; m/$date_qr/ or die "files[$earliest]:$.: invalid line: $_\n"; last if $1 ge $dates[!$earliest]; $i++; } } print "there are $i earlier date entries\n";


        Wow that's great code except the definition of $date_qr = qr line... The extra space lost me an hour or two this morning. I'm not sure why that made a difference. There is a space after the .\d{3} in the input files. Any ideas? Works like a charm besides that!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://983921]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2018-03-17 11:05 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (223 votes). Check out past polls.