Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Time::Piece puzzlement

by hippo (Bishop)
on Sep 15, 2022 at 17:51 UTC ( [id://11146900]=note: print w/replies, xml ) Need Help??


in reply to Time::Piece puzzlement

Question 1: why is it sorting the future date at the top of the list?

Because you are sorting lexically and F comes before T and W.

use strict; use warnings; use Time::Piece; use Time::Seconds; my $dateformat = "%H:%M:%S, %m/%d/%Y"; my $date1 = "11:56:41, 09/14/2022"; my $date2 = "11:22:41, 09/16/2022"; my $date3 = "11:20:41, 09/13/2022"; $date1 = Time::Piece->strptime($date1, $dateformat); $date2 = Time::Piece->strptime($date2, $dateformat); $date3 = Time::Piece->strptime($date3, $dateformat); my(@ds) = ($date1,$date2,$date3); my(@sorted) = sort(@ds); print "Lexically\n"; foreach my $dt (@sorted){ print $dt, $/; } @sorted = sort { $a->epoch <=> $b->epoch } @ds; print "\nTemporally\n"; foreach my $dt (@sorted){ print $dt, $/; }
Question 2: is using Time::Piece->(strptime) the best way to create objects for each of my variables? I didn't see any other way to do that in the doc.

Yes, it probably is the best way. Note that you could use arrays throughout instead of $date1, $date2, $date3, etc.


🦛

Replies are listed 'Best First'.
Re^2: Time::Piece puzzlement
by slugger415 (Monk) on Sep 15, 2022 at 19:44 UTC

    aha of course! thank you.

Log In?
Username:
Password:

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

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

    No recent polls found