Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Max date from set of dates

by ricDeez (Scribe)
on Jul 27, 2011 at 21:58 UTC ( [id://917145]=note: print w/replies, xml ) Need Help??


in reply to Max date from set of dates

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

Replies are listed 'Best First'.
Re^2: Max date from set of dates
by Marshall (Canon) on Jul 27, 2011 at 22:56 UTC
    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"; #----------------------------------------------------------------

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://917145]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-24 02:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found