map { $_->[0] }
sort { $a->[1] cmp $b->[1] }
map {
m!(\d\d)/(\d\d)/(\d{4}):([\d:]+)$!;
[ $_, $3 . $2 . $1 . $4 ]
};
-sauoq
"My two cents aren't worth a dime.";
| [reply] [d/l] |
#!/usr/bin/perl -w
use strict;
my @list= <DATA>;
@list= @list[
map { unpack "N", substr($_,-4) }
sort
map {
join "",
( $list[$_] =~
m[(\d+)/(\d+)/(\d+):([\d:]+)]
)[2,1,0,3],
pack "N", $_;
} 0..$#list
];
print @list;
__END__
G: CCCCC-01 :ADD : ORDER PROCESSED : *** OK ***:08/30/2003:14:24:58
G: CCCCC :MODIFY : ORDER PROCESSED : *** OK ***:08/28/2003:14:24:58
G: CCCC1 :ADD : ORDER PROCESSED : *** OK ***:08/29/2003:14:49:54
- tye
| [reply] [d/l] [select] |
Very nice. Now, I'm sorry, you shouldn't have to hold my hand, but I'm very new to Perl. Could you explain to me how I would I, instead of providing the strings the way you have done it, provide it with a file to read one line at a time and then ouput to another file. Thanks very much.
| [reply] |
Read up on writing your own sorting function. Also, take a look at the Schwartzian Transform. (Writing your own sorting functions is in the same section as sorting in the Camel book. If you have the 3rd edition, look at pp. 789 - 793.) The Schwartzian Transform (aka ST) is searchable in the box at the top of your screen.
------ We are the carpenters and bricklayers of the Information Age. The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6 Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified. | [reply] |
The easiest way I have found to do this is to turn the date and time string into a number. Split the string into day,month,year,hours,minutes,seconds and then feed that into the timelocal function from Time::Local. This will give a number that is easily sorted. Here is a bit of code that describes this somewhat ugly and perhaps not very efficient routine:
#!/usr/bin/perl -w
use strict;
use Time::Local;
my $test = "G: CCCCC-01 :ADD : ORDER PROCESSED : *** OK ***:08/30/2003
+:14:24:58";
my $datetemp = substr($test,index($test,"***:")+4,length($test)-index(
+$test,"***:")+4);
print "DATE & TIME: $datetemp\n";
my $date = substr($datetemp,0,index($datetemp,":"));
print "DATE ONLY: $date\n";
my $time = substr($datetemp,index($datetemp,":")+1,length($datetemp)-i
+ndex($datetemp,":"));
print "TIME ONLY: $time\n";
my @datearray = split(/\//,$date);
my @timearray = split(/:/,$time);
my $ticks = timelocal($timearray[2],$timearray[1],$timearray[0],$datea
+rray[1],$datearray[0]-1,$datearray[2]);
print "CLOCK TICKS SINCE THE EPOCH: $ticks\n";
#usage of the timelocal function
# my $time = timelocal($sec,$min,$hour,$mday,$month,$year);
Again, this is somewhat ugly but it gets the job done in manner that is easy to understand. There is really no need to assign the variables $date and $time, I just thought it would make it easier to understand the process. | [reply] [d/l] |