in reply to sorting a hash by its values, date
This does depend on the day, date and year being 2,2,4 digits, if they are not then a little more needs adding in the map block to fix this
cheers.
#!/usr/local/bin/perl -w use strict; my %h = ( key1 => "10/14/2003", key2 => "11/23/2001", key3 => "12/23/2001", key4 => "12/22/2001", key5 => "02/21/1984", key6 => "08/13/1969", key7 => "09/11/1973", key8 => "09/30/2000" ); my @bydate = map { my @tmp = split /\//, $h{$_}; $tmp[2].":".$tmp[0].":".$tmp[1].":".$_ } keys(%h); foreach (sort @bydate) { my ($yr, $mn, $dy, $key) = split /:/; print "$mn/$dy/$yr = $key\n"; }
#!/usr/local/bin/perl -w use strict; my %h = ( key1 => "10/14/2003", key2 => "11/23/2001", key3 => "12/23/2001", key4 => "12/22/2001", key5 => "02/21/1984", key6 => "08/13/1969", key7 => "09/11/1973", key8 => "09/30/2000" ); my @bydate = map { my @tmp = split /\//, $h{$_}; pack 'A4A2A2A*', $tmp[2], $tmp[0], $tmp[1], $_; } keys(%h); foreach (sort @bydate) { print "@{[unpack ('A4A2A2A*',$_)]}\n"; }
|
|---|