in reply to Re: Max date from set of dates
in thread Max date from set of dates

The sorting by the 3 fields is unnecessary IF the year,month,day fields are of fixed width - meaning leading zeroes are required. If the leading zeroes are not there, then a numeric instead of alphanumeric compare is needed.

The fastest way to do the max date function in the YYYY-MM-DD case is to use List::Util. That is because List::Util and List::Util::More are implemented as XS (C code).

#!/usr/bin/perl -w use strict; # here only maxstr is needed, just showing them all... use List::Util qw(first max maxstr min minstr reduce shuffle sum); my @dates = ( '2011-03-12', '2011-04-09', '2010-12-31', '2010-12-30'); print maxstr(@dates),"\n"; #2011-04-09
This is actually such a big advantage that sometimes it is worth it performance wise to fix "malformed" dates to have the leading zeroes before doing min,max or range searches. The Perl string compare goes quickly.

Update: List::Util is a core module, meaning that it is already on your system without having to install any extra modules.

Replies are listed 'Best First'.
Re^3: Max date from set of dates
by perlVIP (Initiate) on Nov 01, 2013 at 15:17 UTC
    to find latest date in DD-MM-YYYY.
    for (my $i=0;$i<$size;$i++) { #print "\n". $date[$i]; @yeardate[$i] = substr($date[$i],6,4); @monthdate[$i] = substr($date[$i],3,2); @daydate[$i] = substr($date[$i],0,2); } #print "\n array of date is : @date\n"; #print "\n array of year is : @yeardate\n"; #print "\n array of month is : @monthdate\n"; #print "\n array of day is : @daydate\n"; #------------------------------------------------ my $x=-1; $max_year = $yeardate[0]; foreach $yeareach (@yeardate[1..$#yeardate]) {$x++; $max_year= $yeareach if $yeareach gt $max_year; } #print"\n max_year is :" .$max_year. "\n\n"; #------------------------------------------------------------------ $y = 0; $max_month = $monthdate[0]; foreach $montheach (@monthdate[1..$#monthdate]) { $y++; $max_month= $montheach if (($montheach >= $max_month) && ($yeardate[$y +] == $max_year)); } #print"\n max_month is ".$max_month. "\n\n"; #-------------------------------------------------------------- $z = 0; $max_day = $daydate[0]; foreach $dayeach (@daydate[1..$#daydate]) { $z++; $max_day= $dayeach if (($dayeach >= $max_day) && (@yeardate[$z] == $ma +x_year)&& (@monthdate[$z] == $max_month)); } #print"\n".$max_day. "\n\n"; #---------------------------------------------------------------- $latestdate = $max_day."-".$max_month."-"."$max_year"; print "\n\nFolder with latestdate: ". $latestdate. "\n\n"; #----------------------------------------------------------------