#!/bin/perl # triviat.pl --> fixed version of an older version of triviat.pl, # which uses correct # directories for the contest files, # and uses a more robust way of gathering the # filenames of contestants (the glob construct # reached its limits and is now replaced by a # more complex, more robust, readdir routine). $date = `/bin/date`; @date = split(/ +/,$date); # note the + after the space delimiter # with a single-digit day, UNIX prints Fri Dec 1 10:43:57 CST 1995 # which has an extra space in front of the day. That would create # an empty field unless multiple consecutive delimitors were treated # as one # note that the elements of @date are: # 0. Day of Week (ie Wed) # 1. Month (ie Nov) # 2. Number of day (ie 29) # 3. Time (ie 11:50:01) # 4. Time Zone (ie EST) # 5. Year (ie 1995) $qnum = $date[2]; $month = $date[1]; $displaydate = "$date[2] $date[1] $date[5]"; print <<"EndPrn"; Content-TYPE: text/html Trivia Game Top Players

[ @canada | Return to Today's Question ]

Top 10 players win this month's prize. In the event of a tie for 10th place, the winner will be decided by a trivia question sent by e-mail.

EndPrn # The 8 lines o' code below are a more robust way of # dealing with every file in a directory. Beware # that all names of files in the contest directory # are loaded into memory when they are loaded into # the array @tfilelst. If this ever craps out, # you will have to read all of the filenames into # a text file, a name to a line, and then read # the filenames in from that text file and act # upon each one. This, in essence, uses the more # copious (but slower) space on your hard disk, # instead of the smaller (but faster) amount of # RAM available to the Perl script opendir(TRIVDIR,"../data/nmc/nmctrivia/$month") || die "can't open dir $month"; $z = 0; while ($tfile = readdir(TRIVDIR)) { $tfilelst[$z] = $tfile; ++$z; } closedir(TRIVDIR); # in the old days, the line below replaced the 9 lines above # foreach $file (<../data/nmc/nmctrivia/$month/*>) { foreach $file (@tfilelst) { open(EFILE,"../data/nmc/nmctrivia/$month/$file") || die "Cannot read $file"; $inline = ; close(EFILE); @inarray = split(/#/,$inline); $count = 0; foreach $slot (@inarray) { if ($slot eq "r") { ++$count; } } $email = $inarray[0]; if ($players[$count]) { $players[$count] = "$players[$count]
\n$email"; } else { $players[$count] = $email; } } $rcount = 32; until ($rcount == 0) { --$rcount; if ($players[$rcount]) { if ($rcount == 1) { print "

Players with $rcount correct answer

\n"; print "

$players[$rcount]

\n"; } elsif ($rcount != 0) { print "

Players with $rcount correct answers

\n"; print "

$players[$rcount]

\n"; } } } print <<"EndPrnTwo";

[ @canada | Return to Today's Question ]

EndPrnTwo