Re: how to sort hash by value, where value is a valid date



perl_help_needed wrote:
> Hi all,
> I want to sort a hash by its vlaue in perl. Value is a valid date.
> example:
> key value
> 48487 05/05/18
> 52327 05/05/23
> 64998 05/05/24
> 44147 05/05/18

use strict;
use warnings;
use DateTime;
use DateTime::Format::Strptime;
use Data::Dumper;

our $parser =
DateTime::Format::Strptime->new( pattern => '%y/%m/%d' );
my %date_hash=();
while (<DATA>) {
my ($key, $date) = split;
$date_hash{$key} = $parser->parse_datetime( $date );
}
foreach (sort { $date_hash{$a} cmp $date_hash{$b} } keys %date_hash) {
print "Date:$date_hash{$_}\tKey:$_\n";
}


__DATA__
48487 05/05/18
52327 05/05/23
64998 05/05/24
44147 05/05/18


Hope this helps


.



Relevant Pages

  • Re: dns querry script.
    ... use warnings; ... use strict; ... C:\Dload> perl dns.pl ...
    (comp.lang.perl.misc)
  • Re: Any way to access global variable in Perl script from one module file?
    ... use strict; ... use warnings; ... a separate process has a completely separate memory space. ... There is probably no reason to create a new perl ...
    (comp.lang.perl.misc)
  • Re: Debug Help Please
    ... In Perl there is the expression TIMTOWTDI which means that you will probably get different opinions on "The Right Way" to do something in Perl. ... use strict; ... That is a specious argument because you can disable specific strictures or warnings in local scope: ... for all Failures ...
    (perl.beginners)
  • Re: Align Text
    ... On Dec 21, 2003, at 10:47 PM, Bill Jastram wrote: ... use strict; ... use warnings; ... These promise Perl you'll play by the good programmer rules, ...
    (perl.beginners)
  • Re: List Variable becomes undefined inexplicably
    ... warnings and strictures very often find bugs in programs. ... It is inconsiderate of your audience to post without warnings and strict. ... There is a Perl and there is a perl. ... chomp $LINE; ...
    (comp.lang.perl.misc)