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

The date format remains the same: Day DD-MON-YY

The problem is that always before the data was provided to me in such away that the dates were already in the right chronological order, so when my lookup script looked for locations, and printed out the results, they were already in the right order. Now they're not in the right order and I can't get the data provider to fix whatever changed.

I must admit, I don't understand all the code in the example provided, but I'm working through it, attempting to learn.

  • Comment on Re^2: How to re-order badly formed dates within a delimited string?
  • Download Code

Replies are listed 'Best First'.
Re^3: How to re-order badly formed dates within a delimited string?
by CountZero (Bishop) on Jun 15, 2006 at 20:22 UTC
    OK, in that case you must re-sort the file with the list of dates.

    As this involves parsing every record (a potentially "expensive" operation, to be repeated for every comparison in the sort) the "Schwartzian Transform" will come to your rescue:

    use strict; use DateTime::Format::Strptime; my $dtf = new DateTime::Format::Strptime( pattern => '%a %d-%b-%y', lo +cale => 'en_US'); my @events = <DATA>; my @sorted_events = map { $_->[0] } sort { $a->[1] cmp $b->[1] } map { [ $_, $dtf->parse_datetime( (split /\|/)[14])->ymd ] } @events; print @sorted_events; # now you save the array of sorted events and "feed" that to your scri +pt __DATA__ |Puerto Rico||Santiago Apostol Catholic School|Las Croabas Road, Urb S +anta Isidra|Fajardo|PR||00738|Modesto|Garcia|Counselor|787/863-0524|7 +87/863-6655|Sat 09-DEC-06| |Puerto Rico||Santiago Apostol Catholic School|Las Croabas Road, Urb S +anta Isidra|Fajardo|PR||00738|Modesto|Garcia|Counselor|787/863-0524|7 +87/863-6655|Sat 09-JUN-07| |Puerto Rico||Santiago Apostol Catholic School|Las Croabas Road, Urb S +anta Isidra|Fajardo|PR||00738|Modesto|Garcia|Counselor|787/863-0524|7 +87/863-6655|Sat 10-FEB-07| |Puerto Rico||Santiago Apostol Catholic School|Las Croabas Road, Urb S +anta Isidra|Fajardo|PR||00738|Modesto|Garcia|Counselor|787/863-0524|7 +87/863-6655|Sat 14-APR-07| |Puerto Rico||Santiago Apostol Catholic School|Las Croabas Road, Urb S +anta Isidra|Fajardo|PR||00738|Modesto|Garcia|Counselor|787/863-0524|7 +87/863-6655|Sat 28-OCT-06| |Puerto Rico||Santiago Apostol Catholic School|Las Croabas Road, Urb S +anta Isidra|Fajardo|PR||00738|Modesto|Garcia|Counselor|787/863-0524|7 +87/863-6655|Sun 10-DEC-06| |Puerto Rico||Santiago Apostol Catholic School|Las Croabas Road, Urb S +anta Isidra|Fajardo|PR||00738|Modesto|Garcia|Counselor|787/863-0524|7 +87/863-6655|Sun 10-JUN-07| |Puerto Rico||Santiago Apostol Catholic School|Las Croabas Road, Urb S +anta Isidra|Fajardo|PR||00738|Modesto|Garcia|Counselor|787/863-0524|7 +87/863-6655|Sun 15-APR-07| |Puerto Rico||Santiago Apostol Catholic School|Las Croabas Road, Urb S +anta Isidra|Fajardo|PR||00738|Modesto|Garcia|Counselor|787/863-0524|7 +87/863-6655|Sun 29-OCT-06|

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law