August 1999 | Perl Hardware Store | Slide #30 |
Memoizing is very useful for sort comparison functions
Example: Sort strings like May 14, 1997 into chronological order.
Obvious method:
%m2n = ( jan => 0, feb => 1, mar => 2, apr => 3, may => 4, jun => 5, jul => 6, aug => 7, sep => 8, oct => 9, nov => 10, dec => 11, );
sub compare_dates { my ($am, $ad, $ay) = ($a =~ /(\w{3}) (\d+), (\d+)/);
my ($bm, $bd, $by) = ($b =~ /(\w{3}) (\d+), (\d+)/);
$ay <=> $by || $m2n{lc $am} <=> $m2n{lc $bm} || $ad <=> $bd; }
Next | Copyright © 1998 M-J. Dominus |