http://www.perlmonks.org?node_id=983901


in reply to Time matching YYYY-MM-DD HH:MM:SS.SSS

The date format you've shown should be self-sorting, so there shouldn't be any need to parse the dates (barring possible DST issues). Can you give a concrete example (code plus data) where the sorting is "inconsistent"?

Dave.

  • Comment on Re: Time matching YYYY-MM-DD HH:MM:SS.SSS

Replies are listed 'Best First'.
Re^2: Time matching YYYY-MM-DD HH:MM:SS.SSS
by PRyanRay (Novice) on Jul 26, 2012 at 18:38 UTC
    Hi Dave, Here is a bit more code:
    $earliest0 = `more $ARGV[0]| grep -v Epoch > newfile.txt| head -1 newf +ile.txt`; @Array0 = split(' ',$earliest0); $date0 = "@Array0[0] @Array0[1]"; $earliest1 = `more $ARGV[1]| grep -v Epoch > newfile1.txt| head -1 new +file1.txt`; @Array1 = split(' ',$earliest1); $date1 = "@Array1[0] @Array1[1]"; if($date0 eq $date1){$early = $ARGV[0]; $late = $ARGV[1];} if($date0 lt $date1){$early = $ARGV[0]; $late = $ARGV[1]; $File = "newfile.txt"; open(File); $i = 0; while(<File>){ @check = split(' ',$_); $date = "@check[0] @check[1]"; if($date eq $date1){last;} $i++; } } if($date1 lt $date0){$early = $ARGV[1]; $late = $ARGV[0]; $File = "newfile1.txt"; open(File); $i = 0; while(<File>){ @check = split(' ',$_); $date = "@check[0] @check[1]"; if($date eq $date0){last;} $i = $i++; } }
    The  while statements are needed to determine which line number matches the latest date. I tried using regular expressions to do this but could not figure it out. When I execute this script it sometimes returns the correct $i, sometimes executes the wrong $if statement. Thanks!
      Well, I asked for code and data that demonstrates the inconsistency; you didn't show any data.

      Having said that, your code is ... well .. just God-awful.

      It violates just about every rule of good coding practice and style going. I haven't the will to go though a complete list (maybe some other kind soul will have the patience), but just for starters:

      Add

      use warnings; use strict;
      at the start of your code. You will be amazed!

      The two-arg form of open is frowned upon. The one-arg form is vomit inducing.

      In your second branch, you have $i = $i++; which is definitely not what you need, and may in fact be the cause of all your issues.

      Dave.

        The $i = $i++ was a cut and paste error. That's not the problem. It should be $i++;. The data would look something like this:
        file1.txt: 2009/01/01 00:05:00.000 nmbr nmbr 2009/01/01 00:10:00.000 nmbr nmbr 2009/01/01 00:15:00.000 nmbr nmbr 2009/01/01 00:20:00.000 nmbr nmbr file2.txt: 2009/01/01 00:00:00.000 nmbr nmbr 2009/01/01 00:05:00.000 nmbr nmbr 2009/01/01 00:10:00.000 nmbr nmbr 2009/01/01 00:15:00.000 nmbr nmbr 2009/01/01 00:20:00.000 nmbr nmbr
        The user may not know which file contains the earlier timestamp. nmbr is a placeholder. Also, I didn't cut and paste the entire code, it is too long. I can include the preamble for you if you wish
        #!/usr/bin/perl use strict; use warnings;
        What is the form of open I should use? Thanks for the help.