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:
Date Sort

 



Champion
Deleted

Feb 7, 2000, 12:49 PM

Post #1 of 6 (754 views)
Date Sort Can't Post

I am having a problem sorting on the date field. Can somebody help?

open(FILE, "data.dat") | | die "I can't open that because: $!\n";
while(<FILE> ) {
chop;
@all = split(/\n/);
foreach $line (@all) {
local($rnum, $teacher, $title, $date, $message) = split(/&&/, $line);
if ($keyword eq "$rnum") {
$match = 1;
print "<li><b>$date $title</b><br>$message<p>\n";
}
}
}
close(FILE);
print "</ul></form>\n";
&html_footer;
exit0;
}

I want my script to list all of the messages that meet the $rnum criteria. Then I want to print it in order by the $date field. Can somebody help?


ljfaraci
Deleted

Feb 9, 2000, 1:56 PM

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

Here's what I would do:

1. Declare an array:
%DateArray = {};

2. Inside your "if" block I would add a single line to load it with lines that meet the $rnum criteria:
$DateArray {$date} = $line;

3. Then after the "close file" statement, I would add something like the following:

foreach $date (sort (keys (%DateArray)))
{print $DateArray {$date} "\n";}

hope this helps... LJF...


Cure
User

Feb 10, 2000, 8:20 AM

Post #3 of 6 (754 views)
Re: Date Sort [In reply to] Can't Post

NOTE:

%DateArray = {};
This will attempt to assign a hash reference to a hash.


@DateArray = ();
This declares an array.


Cure


ljfaraci
Deleted

Feb 10, 2000, 2:22 PM

Post #4 of 6 (754 views)
Re: Date Sort [In reply to] Can't Post

Sorry, I meant declare a hash... 8-(



Champion
Deleted

Feb 15, 2000, 5:03 PM

Post #5 of 6 (754 views)
Re: Date Sort [In reply to] Can't Post

I get an error message everytime I try to put %DateArray = {}; or @DateArray = ();
in my script. Can somebody tell me why or show me where to declare the DateArray at?


ljfaraci
Deleted

Feb 23, 2000, 8:14 AM

Post #6 of 6 (754 views)
Re: Date Sort [In reply to] Can't Post

I'm sorry, you need to use parenthesis for the declaration....

examples:

my %DateArray = ();
or
local %DateArray = (); # if needed in subroutines.

 
 


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

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