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

Re: Need help comparing 4 dates

by ig (Vicar)
on Oct 02, 2010 at 23:17 UTC ( [id://863120]=note: print w/replies, xml ) Need Help??


in reply to Need help comparing 4 dates

I'm not sure why you say Date_Cmp doesn't recognize the date formats. Maybe because your test of $flag isn't doing what you think it should. $flag can be -1, 0 or 1. Your output suggests the same result whenever the dates for Bob and Jim are different, regardless of which is the earlier date, and something else when they are the same. You might try the test $flag > 0 and see if that gives you the result you are expecting.

Replies are listed 'Best First'.
Re^2: Need help comparing 4 dates
by dirtdog (Monk) on Oct 03, 2010 at 03:37 UTC

    Thanks to all for the replies, but I guess my post was a little confusing. My question really is does anyone have a recommendation for the best way to compare 4 dates and then print the order. Comparing 2 dates is easy, but it gets tricky when comparing 4 dates and displaying the order from the earliest to latest as below:

    EVENT JIM BOB SAM JACK PTRED 4 3 1 2 RED 3 1 2 4 INT 3 4 2 1 PTRED 2 1 4 3 RED 2 1 3 4 INT 4 3 1 2

    I got some ideas from ww and will give it a try. Thanks

      Given a list of dates, to calculate a corresponding list of ordinals I might do something like the following:

      # Make a list of tuples: (index, date) my @list = map { [ $_, $dates[$_] ] } (0..$#dates); # Sort the list by date @list = sort { Date_Cmp($a->[1], $b->[1]) } @list; # Determine the corresponding ordinal numbers my @ordinals; foreach (0..$#dates) { $ordinals[ $list[$_]->[0] ] = $_ + 1; }

      I don't find this very elegant - there may be much better ways. There may be a module that does this, though I don't know one.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-19 23:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found