CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Sorting arrays by date

 



benn600
User


Apr 10, 2005, 9:45 AM

Post #1 of 8 (1876 views)
Sorting arrays by date Can't Post

My array contains the date in a value such as the 4th item... $zz|$zz|$zz|$date and I would like to know if I can sort this by $date without making a new array and moving date to the beginning?

My real problem is about how to sort by date. My date fields look like this: Apr. 4 at 2:42 PM :and I am now learning that I will have to add a year to that so sorting will work better. Therefore, I'd like to sort by this:

Apr. 4 2005 at 2:42 PM

and put them in order by date and time! Do you have any suggestions for how to add the year? Simply adding 2005 wouldn't be exceptional because that is a bit too long for the place I am using it in. Perhaps simply 05 would work or what if I changed it completely to 4-4-05 ? I'd like to make this as easy as possible, but making changes like that will just add time until I can implement this feature into my web site.
----------------------------------------------------------------------------
Wink http://www.ppcpathways.com/ Wink
Visit my new site devoted to reviewing the latest pocketpc products and news that I built in cgi-perl!

(This post was edited by benn600 on Apr 13, 2005, 10:35 AM)


KevinR
Veteran


Apr 10, 2005, 3:34 PM

Post #2 of 8 (1874 views)
Re: [benn600] Sorting arrays by date [In reply to] Can't Post

store the date in machine format, the epoch. Like:

$date = time;

then you can easily sort dates and display dates and anything else you might need to do with dates. You can also sort your array using any part of your data you want to without having to change the structure or create new arrays.
-------------------------------------------------


benn600
User


Apr 10, 2005, 4:42 PM

Post #3 of 8 (1873 views)
Re: [KevinR] Sorting arrays by date [In reply to] Can't Post

That is amazing! I did not know about that. Now how do I get it converted into a better format for viewing? I cannot believe I missed that! That makes dates so much easier in perl.
----------------------------------------------------------------------------
Wink http://www.ppcpathways.com/ Wink
Visit my new site devoted to reviewing the latest pocketpc products and news that I built in cgi-perl!


MrPJ
User

Apr 10, 2005, 4:51 PM

Post #4 of 8 (1872 views)
Re: [benn600] Sorting arrays by date [In reply to] Can't Post

use POSIX qw/strftime/;

my $date = time();
print strftime("%d %m %Y, %H:%M:%S", localtime($date));


benn600
User


Apr 10, 2005, 8:53 PM

Post #5 of 8 (1867 views)
Re: [MrPJ] Sorting arrays by date [In reply to] Can't Post

Do you have a list of all the different variables? I noticed that %p is AM or PM. What is the letter to use for hour in 12-hour mode?
----------------------------------------------------------------------------
Wink http://www.ppcpathways.com/ Wink
Visit my new site devoted to reviewing the latest pocketpc products and news that I built in cgi-perl!


KevinR
Veteran


Apr 10, 2005, 9:43 PM

Post #6 of 8 (1866 views)
Re: [benn600] Sorting arrays by date [In reply to] Can't Post

you can also just use localtime() to display preformatted dates. Say you already have a date stored in $date:

$date_to_display = localtime($date);

or you can also do this if you just want to display the current date/time:

$date_to_display = localtime(time);

but the way Mr.PJ showed you has easy options for formatting the date/time anyway you want to.
-------------------------------------------------


davorg
Thaumaturge / Moderator

Apr 13, 2005, 5:45 AM

Post #7 of 8 (1854 views)
Re: [benn600] Sorting arrays by date [In reply to] Can't Post


In Reply To
Do you have a list of all the different variables? I noticed that %p is AM or PM. What is the letter to use for hour in 12-hour mode?


I googled for strftime. The first link seems to contain exactly what you want.

You know, it's often much faster to look for answers yourself :)

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


benn600
User


Apr 13, 2005, 10:38 AM

Post #8 of 8 (1850 views)
Re: [davorg] Sorting arrays by date [In reply to] Can't Post

I actually just figured it out on my own by trying all the letters, lowercase and uppercase. This allowed me to find options that I probably wouldn't have found if I just got an answer to my question, that I is used for 12-hour format time--so I am glad that I didn't get an answer.
----------------------------------------------------------------------------
Wink http://www.ppcpathways.com/ Wink
Visit my new site devoted to reviewing the latest pocketpc products and news that I built in cgi-perl!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives