Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

i am trying to find the max date from list of dates in an array without using any module.

but i am doing only string compare. also i tried in super search, i didn't find.

is there any method available for finding max date from collection of dates?

--Sam

Replies are listed 'Best First'.
Re: Max date from set of dates
by JavaFan (Canon) on Jul 27, 2011 at 20:17 UTC
    I'm assuming all your dates are in YYYY-MM-DD format (or any other consistent format that allows for easy comparing). In that case, you could use:
    my $max_date = $dates[0]; foreach $date (@dates[1..$#dates]) { $max_date = $date if $date gt $max_date; }
Re: Max date from set of dates
by Tanktalus (Canon) on Jul 27, 2011 at 20:12 UTC
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Max date from set of dates
by ricDeez (Scribe) on Jul 27, 2011 at 21:58 UTC

    This is one way...

    my @dates = ( '2011-04-09', '2011-03-12', '2010-12-31', '2010-12-30'); my @sorted_dates = sort { (split /\-/, $a)[0] <=> (split /\-/, $b)[0] +|| (split /\-/, $a)[1] <=> (split /\-/, $b)[1] +|| (split /\-/, $a)[2] <=> (split /\-/, $b)[2] +} @dates; my $max_date = @sorted_dates[-1]; print "$max_date";
      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.

        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"; #----------------------------------------------------------------