In article <[EMAIL PROTECTED]>, [EMAIL PROTECTED] (Dale Pellerin) wrote:
> I am very new to hashes (as well as Perl) and am having some difficulty with > a cgi script. This script allows individuals to post "announcements" via a > form. The form data is stored in a text file, sorted in descending order by > date, and printed to the html page. However, the script is currently > sorting by the month and not by the year, thus creating a problem when news > items are posted from 2002. (01/01/02 news items are being printed below > 12/01/01 news items) > > Here is a sample of the text file that the data is stored in: > > http://is-blah.com/corprate/pages/news/cafeteria_closed.htm ~ 10/24/01 ~ > Cafeteria closed > > Here is a snippet of code from the cgi script that sorts the file: > > # Hash to sort date > $from = "tmc.dat"; > my %h; > > open(FILE, "$from") || die "Can't open $from!\n"; > while (<FILE>) { > chomp; > ($announcement,$date,$description) = split(/~/,$_); > > # if the date has already been seen, tack this entry on to the hash value > if( $h{$date} ){ > $h{$date} .= "|$announcement~$description"; > } > > # date hasn't been seen, so create a new hash entry > else > { > $h{$date} = "$announcement~$description"; > } > } > > #sort the dates in desc. order > foreach $key (sort {$b cmp $a} keys %h) > { > #do for each item for that date > @items = split '\|', $h{$key}; > foreach $item (@items) > { > #split back out the values and print the table > my($announcement,$description) = split /~/,$item; > print "<table width=800><tr>"; > print "<td><img border=0 width=14 height=14 id='_x0000_i1027' > src='http://is-web/corprate/gifs/blueball.gif'>"; > print "<ALIGN='left'><span style='font-size:13.5pt'><a > href=\"$announcement\"><font color=blue>$description</font></a>(<font > color=black> $key)</font></td>"; > print "</tr></table><br>"; > > I would appreciate any assistance in figuring out how to sort by year, then > by month. Child's play. Under the "Efficiency" secion in the Camel[1], there is a section that reads: "Sorting on a manufactured key array may be faster than using a fancy sort subroutine. A given array value will usually be compared multiple times, so if the sort subroutine as to do much recalculation, it's better to factor out that calculation to a separate pass before the actual sort." ... and at this point tiny little bells should be going off in your head. :-) right around here: use POSIX qw/strftime/; > while (<FILE>) { > chomp; > ($announcement,$date,$description) = split(/~/,$_); > my ($mon, $day, $yr) = split /\//, $date;# non european date $date = strftime("%Y/%m/%d", 0, 0, 0, $day, $mon - 1, $yr + 100 ); #or # $date = strftime("%y/%m/%d", 0, 0, 0, $day, $mon - 1, $yr ); then the date will sort by, say, 2001/10/24 or 01/10/24 although I argue for the former and not the latter. I've had to do similar things when converting an old database to MySQL's DATETIME format. YYYY-MM-DD 00:00:00 I'd suggest reformatting those dates if it all possible, and not too inconvenient. A true database would in the long run be better, if such can be arranged (IMHO). [1]Programming Perl, both 2nd and 3rd editions mention this print pack "H*", "4a75737420416e6f74686572204d61635065726c204861636b65722c0d"; -- Scott R. Godin | e-mail : [EMAIL PROTECTED] Laughing Dragon Services | web : http://www.webdragon.net/ It is not necessary to cc: me via e-mail unless you mean to speak off-group. I read these via nntp.perl.org, so as to get the stuff OUT of my mailbox. :-) -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]