in reply to Is there an easier way?

#!/usr/bin/perl use strict; my $first_date = '12/14/2000'; my $second_date = '04/18/2001'; print isGreatest($first_date,$second_date); sub isGreatest { # strip data into arrays my @date_1 = split /\//, $_[0]; my @date_2 = split /\//, $_[1]; # compare ordered date strings my $result = sprintf("%04d%02d%02d",@date_1[2,0,1]) <=> sprintf("%04d%02d%02d",@date_2[2,0,1]); # $result is -1, 0 or 1, so amend for output # to return only 0 or 1; return ($result + $result**2)/2; }

(s)printf is your ally, but use it wisely. Read the (s)printf tutorial elsewhere in the monastry for a deeper insight.

cLive ;-)

Update: interesting thought chipmunk, never thought of that. d'oh!

Replies are listed 'Best First'.
Re: I'd use something like this...
by chipmunk (Parson) on May 01, 2001 at 07:28 UTC
    # compare ordered date strings my $result = sprintf("%04d%02d%02d",@date_1[2,0,1]) <=> sprintf("%04d%02d%02d",@date_2[2,0,1]); # $result is -1, 0 or 1, so amend for output # to return only 0 or 1; return ($result + $result**2)/2;
    Hmm... That's a neat trick with exponentiation to convert -1 to 0, but wouldn't this be much simpler?
    # compare ordered date strings return sprintf("%04d%02d%02d",@date_1[2,0,1]) > sprintf("%04d%02d%02d",@date_2[2,0,1]);