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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate:
Sort by Date



Aug 9, 2000, 12:21 AM

Post #1 of 2 (1054 views)
Sort by Date Can't Post

Hello there!

Here's one that's really gonna fry your brains! :-)

One of the fields in my flat database is the date the record was created. The date is in the following format:

7 August 2000

Now then... How can I put an icon by the record if the record has been created in the last 10 days?

I'm absolutely stump about this one, so any help would be greatly appreciated. I must learn perl oen of these days <g>...

I know I need to convert the date back to unix format (or unless I'm guessing I need to) and then compare the date with the current date on my box?




Aug 9, 2000, 3:39 AM

Post #2 of 2 (1054 views)
Re: Sort by Date [In reply to] Can't Post


I did something similar myself recently.

First of all you need to convert the date into a format that can be sorted numerically.

27/7/00 would need to be encoded as

In your case
($date,$monthWord,$year) = split(/ /,$thing);

if (length($date) == 1) {
$data = "0".$date;

so the date is now the required 2 digit number.

Now you need the hash

'January', '01',

... etc.

You can use this hash to convert $monthWord

$monthNum = $MONTHS{$monthWord};

$sortable = $year.$monthNum.$date;

Now is the sorting.
I use a simple bubblesort that is terribly implemented (because I was in a rush and couldn't be bothered. It works, so I haven't fixed it).

Here's what I do (which is probably wrong)

1) Get the records from my database and store
each in an array recordN
where N is a different number for each record.

2) Create an array where entry i is i with 1 entry for each database record. Call this @array

This is my bubblesort
$exit = 0;
while( $exit == 0) {
$exit = 1;
for($i=0;$i<$#array; $i++) {
$pointer = "record".$array[$i];
$pointerr = "record".$array[$i+1];
# suppose the date is in field 3
if ($$pointer[3] gt $$pointerr[3]) {
$temp = $array[$i];
$array[$i] = $array[$i+1];
$array[$i+1] = $temp;
$exit = 0;

this code is off the top of my head, but you get the picture.
At the end of it all @array gives the numbers to append to "record" in the sorted order.


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

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