astridv: (Default)
astridv ([personal profile] astridv) wrote2005-11-07 12:54 pm

another little puzzle (sorry for the puzzlespam)

I'm trying to fix the 'random story' option in the archive. The script basically works fine, but since a number of stories aren't stored onsite, there'll be an error message anytime the archive randomly picks one of those. There're about 100 offsite links, which would make for a quota of 25% error messages. So, really messy.

I thought it should't be too hard to fix since I just have to tell it to keep picking random numbers until it lands on a story. Here's the part in question; the bold lines are the ones added by me.

ETA: Ha. Figured it out. Haha. Hahaha. It's working! My code is actually working! And I don't know the first thing about Perl. (The permissions were the problem, in case anyone is curious *grins*)



&CgiDie ("Not enough files!", "Not enough files to choose one!") unless (%FILES and (scalar keys %FILES > 1));
$total_stories=scalar keys %FILES;
do {
$random_key=&random_int (1,$total_stories);
} until (exists $FILES{$random_key}{Location});
$partial_location=$FILES{$random_key}{Location};
$file_location="$ARCHIVEURL/$partial_location" . ".html";

My problem is that I'm getting an internal server error. What is more, I'm getting that error even when I just download the random.cgi script and upload it again unchanged. So I think the problem may not be my code at all. Plus, I'd uploaded it, with my changes, to my test archive and that didn't choke. I'm stumped.

(Sorry, friends list... it shall not pester you with these requests much longer, promise. But I gotta figure this out, stuff like this is driving me crazy.)


#! /usr/bin/perl -T -I. 
# Random Story Selector, v. 1.1
# by Sarah T. (harriet_spy@yahoo.com)
# This script works with Naomi Novik's Automated Archive package
# to return a randomly-selected story from an AA archive.

use AutomatedArchiveSettings;
unshift (@INC, @LIBDIR);
require 'archiveLib.pl';
require 'cgi-lib.pl';

my ($total_stories, $random_key, $file_location, $partial_location);

&readDB; # this reads in the hash that indexes stories

#next, we pick a random key number and pull the location of the story associated with it

&CgiDie ("Not enough files!", "Not enough files to choose one!") unless (%FILES and (scalar keys %FILES > 1));
$total_stories=scalar keys %FILES; 
do {
$random_key=&random_int (1,$total_stories); 
}  until (exists $FILES{$random_key}{Location});
$partial_location=$FILES{$random_key}{Location};
$file_location="$ARCHIVEURL/$partial_location" . ".html";

#then, we redirect the user to it



print "Location: $file_location\n";
print "\n";	

#subroutine lifted from perlfaq, of course

sub random_int  {
     my($min, $max) = @_;
     return $min if $min == $max;
     ($min, $max) = ($max, $min)  if  $min > $max;
     return $min + int rand(1 + $max - $min);
   }  
;


Post a comment in response:

(will be screened)
(will be screened if not validated)
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

If you are unable to use this captcha for any reason, please contact us by email at support@dreamwidth.org