in reply to How to re-order badly formed dates within a delimited string?

Well ... getting the DBA to fix it should be the first step ... is there a SQL statement that you control for retrieving the data? If so, double checking for an ORDER BY clause would be helpful.

How big are the data files and how constant is the date field format? If small and always the same, you could go with the real cheap and easy approach by first reading in the data and then sorting it yourself. But first you need to get the date into a string easy to sort:

my %months = ( JAN => '01', FEB => '02', MAR => '03', APR => '04', MAY => '05', JUN => '06', JUL => '07', AUG => '08', SEP => '09', OCT => '10', NOV => '11', DEC => '12' ); my %records; while( $line = <DATA> ) { chomp( $line ); my @line = split( /\|/, $line ); my @date = split( /-/, $line[-1] ); # bad practice here .. hopefully no dates from # last century (or next) my $year = "20" . $date[2]; my $mon = $months{$date[1]}; my $day = (split( /\s+/, $date[0]))[1]; # push onto array in case multiple events for date push( @{$records{$year.$mon.$day}}, $line ); } foreach my $date ( sort keys %records ) { foreach my $record ( @{$records{$date}} ) { # do your thing } }
that's pretty brittle code but may be good enough for your needs

-derby

Replies are listed 'Best First'.
Re^2: How to re-order badly formed dates within a delimited string?
by hmbscully (Scribe) on Jun 15, 2006 at 15:20 UTC

    I have no control over the data in the DB. I get the data pushed to me by a DBA, I can't touch the SQL. The files are relatively small and the data field format is consistent across them all. I'll give it a whirl. Might just have to be a stop-gap until the DBA figures out the real issue.

    I learn more and more about less and less until eventually I know everything about nothing.