in reply to Re: Sort directories by date
in thread Sort directories by date

Yeah, but why would you want to populate a hash and use sort, when you already have everything at hand and only need one very small extra step to find the max value?
#!/usr/bin/env perl use strict; use warnings; use feature qw (say); my @dates = qw{ 12112014 01052015 02202015 03102015 01012011 04092015 09092015 }; my $max_date = 0; my $result; for (@dates) { my $date = join "", reverse unpack q(a4a4); $result = $_ and $max_date = $date if $date > $max_date; } print $result;
Update: fixed a mistake (missing reverse) in the my $date = ... code line above. Thanks to poj for pointing out the error.

Replies are listed 'Best First'.
Re^3: Sort directories by date
by karlgoethebier (Abbot) on Sep 30, 2015 at 10:16 UTC
    "...why would you want to populate a hash and use sort..."

    Because i had no better idea :-(

    Update: No, wait:

    use strict; use warnings; use feature qw (say); use List::Util qw(max); use Time::Piece; my @dates = qw{ 12112014 01052015 02202015 03102015 01012011 04092015 09092015 }; say localtime( max map { Time::Piece->strptime( $_, "%m%d%Y" )->epoch +} @dates ) ->strftime("%m%d%Y"); __END__ \Desktop\monks>other_idea.pl 09092015

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»