On Jul 3, [EMAIL PROTECTED] said:

>My hash has keys that are dates in "ddmmmyyyy" format (21Jul2001).

The date format DDMMYYYY is easily sortable (even via normal 'cmp'
comparison).  Translate your dates into this format:

  %mon2num = qw(
    Jan 01 Feb 02 Mar 03 Apr 04 May 05 Jun 06
    Jul 07 Aug 08 Sep 09 Oct 10 Nov 11 Dec 12
  );

  %num2mon = reverse %mon2num;  # so we can go backwards, if desired

  @sorted_dates =
    map { s/(\d\d)(\d\d)/$1$num2mon{$2}/; $_ }  # change MM -> MON
    sort                                        # sort the dates
    map { s/(\D+)/$mon2num{$1}/; $_ }           # change MON -> MM
    @dates;

This is a Guttman-Rosler Transform (it might look like a Schwartzian
Transform, but it is not).  The GRT works by finessing the data into a
form that can be sorted by simply saying sort(), and then finessing it
back to how you want.

This assumes your dates are DDMMMYYYY.  If you have a DMMMYYYY (like
2Jul2001), you'll need to pad it with a 0.

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
I am Marillion, the wielder of Ringril, known as Hesinaur, the Winter-Sun.
Are you a Monk?  http://www.perlmonks.com/     http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc.     http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter.         Brother #734
**      Manning Publications, Co, is publishing my Perl Regex book      **

Reply via email to