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: Intermediate:
Date Sorting.

 



JDogRob
New User

Aug 27, 2011, 8:22 PM

Post #1 of 2 (1261 views)
Date Sorting. Can't Post

Hi All -

New to Perl but picking it up quickly. Need help with a solution.

I have two files both with two timestamps in different formats.

File1 Example:

USN, COT, TPZ, 2012/58:20:36:41, 2012/58:20:42:19, COT, 75, S1

File2 Example:

000002 T171W 054:04:25:49 00:53:53 054:05:19:43 S1 Comment

I parse each file and and perform a localtime function on the parsed values as a list context and then appropriately assign each of those values to get the correct formatted values out.

Output File example:

@sc:StartRTSCommand rtsID = 176 $time = 2012/02:28:20:31:41

My problem is that I need to sort these times. Each file individually is sorted but I need to combine and push to a new output file (as seen above).

To add insult I would prefer not to use CPAN modules for future generations to maintain.

Any help is appreciated.


Code
 
#! /usr/bin/perl

use strict;
use warnings;
use Time::Local 'timelocal_nocheck';

open USN, "<", "/Users/Jason/Desktop/FDS_COTS_USN_18Aug2011_135508.txt" or die $!;
open TDRSS, "<", "/Users/Jason/Desktop/FDS_COTS_TDRSS_18Aug2011_134843.txt" or die $!;

my @AllUSNPasses;

my @mon = qw(01 02 03 04 05 06 07 08 09 10 11 12);

my @day = qw(01 02 03 04 05 06 07 08 09 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31);

my @min = qw(00 01 02 03 04 05 06 07 08 09
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47 48 49
50 51 52 53 54 55 56 57 58 59
60);

my @sec = qw(00 01 02 03 04 05 06 07 08 09
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47 48 49
50 51 52 53 54 55 56 57 58 59
60);


my @hour = qw(00 01 02 03 04 05 06 07 08 09
10 11 12 13 14 15 16 17 18 19
20 21 22 23);


my %communicationPassHash;
my @finalCommunicationPassArray;
my @ndnewArray;
my @mynewarray;
my @rdnewArray;
my $count;

$count = 0;
my @bigArray;


while (my $parsedUSNline = <USN>){
$count = $count + 1;
chomp($parsedUSNline);
$parsedUSNline =~ s/,//g;
my @line = split (/ /, $parsedUSNline);

$line[3] =~ s/\//:/;
my @groundAOStimeStamp = split(":", $line[3]);

$line[4] =~ s/\//:/;
my @groundLOStimeStamp = split(":", $line[4]);



my @groundAOStimeStampATSCmd1 = localtime timelocal_nocheck $groundAOStimeStamp[4] - 300, $groundAOStimeStamp[3], $groundAOStimeStamp[2],
$groundAOStimeStamp[1],0, $groundAOStimeStamp[0];


my @groundAOStimeStampATSCmd2 = localtime timelocal_nocheck $groundAOStimeStamp[4] - 60, $groundAOStimeStamp[3], $groundAOStimeStamp[2],
$groundAOStimeStamp[1], 0, $groundAOStimeStamp[0];


my @groundAOStimeStampATSCmd3 = localtime timelocal_nocheck $groundAOStimeStamp[4] - 20, $groundAOStimeStamp[3], $groundAOStimeStamp[2],
$groundAOStimeStamp[1], 0, $groundAOStimeStamp[0];


my @groundLOStimeStampATSCmd4 = localtime timelocal_nocheck $groundLOStimeStamp[4] + 20, $groundLOStimeStamp[3], $groundLOStimeStamp[2],
$groundLOStimeStamp[1], 0, $groundLOStimeStamp[0];


my @groundLOStimeStampATSCmd5 = localtime timelocal_nocheck $groundLOStimeStamp[4] + 60, $groundLOStimeStamp[3], $groundLOStimeStamp[2],
$groundLOStimeStamp[1], 0, $groundLOStimeStamp[0];


my @groundLOStimeStampATSCmd6 = localtime timelocal_nocheck $groundLOStimeStamp[4] + 70, $groundLOStimeStamp[3], $groundLOStimeStamp[2],
$groundLOStimeStamp[1], 0, $groundLOStimeStamp[0];







print ('@sc:StartRTSCommand rtsID = 176 $time = ', $groundAOStimeStampATSCmd1[5] += 1900, "/", $mon[$groundAOStimeStampATSCmd1[4]], ":", $day[$groundAOStimeStampATSCmd1[3]], ":", $hour[$groundAOStimeStampATSCmd1[2]], ":", $min[$groundAOStimeStampATSCmd1[1]], ":", $sec[$groundAOStimeStampATSCmd1[0]], "\n");

print ('@sc:StartRTSCommand rtsID = 274 $time = ', $groundAOStimeStampATSCmd2[5] += 1900, "/", $mon[$groundAOStimeStampATSCmd2[4]], ":", $day[$groundAOStimeStampATSCmd2[3]], ":", $hour[$groundAOStimeStampATSCmd2[2]], ":", $min[$groundAOStimeStampATSCmd2[1]], ":", $sec[$groundAOStimeStampATSCmd2[0]], "\n");


print ('@sc:StartRTSCommand rtsID = 268 $time = ', $groundAOStimeStampATSCmd3[5] += 1900, "/", $mon[$groundAOStimeStampATSCmd3[4]], ":", $day[$groundAOStimeStampATSCmd3[3]], ":", $hour[$groundAOStimeStampATSCmd3[2]], ":", $min[$groundAOStimeStampATSCmd3[1]], ":", $sec[$groundAOStimeStampATSCmd3[0]], "\n");


print ('@sc:StartRTSCommand rtsID = 175 $time = ', $groundLOStimeStampATSCmd4[5] += 1900, "/", $mon[$groundLOStimeStampATSCmd4[4]], ":", $day[$groundLOStimeStampATSCmd4[3]], ":", $hour[$groundLOStimeStampATSCmd4[2]], ":", $min[$groundLOStimeStampATSCmd4[1]], ":", $sec[$groundLOStimeStampATSCmd4[0]], "\n");


print ('@sc:StartRTSCommand rtsID = 269 $time = ', $groundLOStimeStampATSCmd5[5] += 1900, "/", $mon[$groundLOStimeStampATSCmd5[4]], ":", $day[$groundLOStimeStampATSCmd5[3]], ":", $hour[$groundLOStimeStampATSCmd5[2]], ":", $min[$groundLOStimeStampATSCmd5[1]], ":", $sec[$groundLOStimeStampATSCmd5[0]], "\n");


print ('@sc:StartRTSCommand rtsID = 272 $time = ', $groundLOStimeStampATSCmd6[5] += 1900, "/", $mon[$groundLOStimeStampATSCmd6[4]], ":", $day[$groundLOStimeStampATSCmd6[3]], ":", $hour[$groundLOStimeStampATSCmd6[2]], ":", $min[$groundLOStimeStampATSCmd6[1]], ":", $sec[$groundLOStimeStampATSCmd6[0]], "\n");



}






while (my $parsedTDRSSline = <TDRSS>){
if ($parsedTDRSSline =~ /^\d/){



my @parsedTDRSSline = split (/ +/, $parsedTDRSSline);

my @tdrssAOStimeStamp = split(/:/, $parsedTDRSSline[2]);

my @tdrssLOStimeStamp = split(/:/, $parsedTDRSSline[4]);

my @tdrssAOStimeStampATSCmd1 = localtime timelocal_nocheck $tdrssAOStimeStamp[3] - 60, $tdrssAOStimeStamp[2],$tdrssAOStimeStamp[1],$tdrssAOStimeStamp[0], 0, 112;
my @tdrssAOStimeStampATSCmd2 = localtime timelocal_nocheck $tdrssAOStimeStamp[3] - 20, $tdrssAOStimeStamp[2],$tdrssAOStimeStamp[1],$tdrssAOStimeStamp[0], 0, 112;
my @tdrssLOStimeStampATSCmd3 = localtime timelocal_nocheck $tdrssLOStimeStamp[3] + 20, $tdrssLOStimeStamp[2],$tdrssLOStimeStamp[1],$tdrssLOStimeStamp[0], 0, 112;
my @tdrssLOStimeStampATSCmd4 = localtime timelocal_nocheck $tdrssLOStimeStamp[3] + 30, $tdrssLOStimeStamp[2],$tdrssLOStimeStamp[1],$tdrssLOStimeStamp[0], 0, 112;




print "############################## TDRSS PASS ################################\n";

print ('@sc:StartRTSCommand rtsID = 271 $time = ', $tdrssAOStimeStampATSCmd1[5] += 1900, "/", $mon[$tdrssAOStimeStampATSCmd1[4]], ":", $day[$tdrssAOStimeStampATSCmd1[3]], ":", $hour[$tdrssAOStimeStampATSCmd1[2]], ":", $min[$tdrssAOStimeStampATSCmd1[1]], ":", $sec[$tdrssAOStimeStampATSCmd1[0]], "\n");

print ('@sc:StartRTSCommand rtsID = 268 $time = ', $tdrssAOStimeStampATSCmd2[5] += 1900, "/", $mon[$tdrssAOStimeStampATSCmd2[4]], ":", $day[$tdrssAOStimeStampATSCmd2[3]], ":", $hour[$tdrssAOStimeStampATSCmd2[2]], ":", $min[$tdrssAOStimeStampATSCmd2[1]], ":", $sec[$tdrssAOStimeStampATSCmd2[0]], "\n");

print ('@sc:StartRTSCommand rtsID = 269 $time = ', $tdrssLOStimeStampATSCmd3[5] += 1900, "/", $mon[$tdrssLOStimeStampATSCmd3[4]], ":", $day[$tdrssLOStimeStampATSCmd3[3]], ":", $hour[$tdrssLOStimeStampATSCmd3[2]], ":", $min[$tdrssLOStimeStampATSCmd3[1]], ":", $sec[$tdrssLOStimeStampATSCmd3[0]], "\n");

print ('@sc:StartRTSCommand rtsID = 272 $time = ', $tdrssLOStimeStampATSCmd4[5] += 1900, "/", $mon[$tdrssLOStimeStampATSCmd4[4]], ":", $day[$tdrssLOStimeStampATSCmd4[3]], ":", $hour[$tdrssLOStimeStampATSCmd4[2]], ":", $min[$tdrssLOStimeStampATSCmd4[1]], ":", $sec[$tdrssLOStimeStampATSCmd4[0]], "\n");



}







}



BillKSmith
Veteran

Aug 27, 2011, 9:35 PM

Post #2 of 2 (1252 views)
Re: [JDogRob] Date Sorting. [In reply to] Can't Post

Note: Validation and maintenance are the best reasons FOR[ using a module such as Date::Calc.



I doubt that you will get much detailed help duplicating a function which already exists unless you make a very good case for why that is important.

If you really want to try it yourself:

Write a regular expression for each date format which will extract the individual fields.

Compute the time in seconds from the start of some arbitrary date. (I recommend that you use the same one as you operating system.) Remember to use the correct number of days for each month (Including February in leap years). It is probably safe to ignore leap-second corrections. You will be dealling with rather large integers. You have to be careful about overflow and roundoff issues. Things get much more complicated if you have do deal with dates more than 200 years old because they may be from a Julian (not Gregorian) calander.

Sort based on that time.

This is a very error-prone task. Validation is imperative! Study the validation of Date modules on CPAN for hints on how to do this.
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