Gossamer Forum
Home : General : Perl Programming :

CGI Script writing challenge

Quote Reply
CGI Script writing challenge
I think this will take one of you PERL guru's about 10 minutes to write.

I am looking for someone to post a script that can do what this site does:

http://www.thesurrealist.co.uk/slogan.cgi

It does not need to look pretty. I can clean up the html to make it look good. Also, I'll put in the extra "slogans."

As I see it the script would just make the user input a variable that would be put in a randomized phrase from a preset number of phrases or "slogans." Then display just it.

I think this would be easy but it is beyond my programming skills.

I want to use this for a non-profit kids' web site that I maintain in my spare moments (late at night or weekends). That's why I am asking for a volunteer to do this.

Thank you in advance for your help.

--Curt Gunz

http://ChurchFun.com
Quote Reply
Re: [Curt] CGI Script writing challenge In reply to
So this "challenge" is just a way for you to get a free custom written script, right? Wink
Quote Reply
Re: [Paul] CGI Script writing challenge In reply to
In Reply To:
So this "challenge" is just a way for you to get a free custom written script, right? Wink


I thought I was being up front about that at the end of my post.

Yes, I am asking for someone to write a script.Smile
Quote Reply
Re: [Curt] CGI Script writing challenge In reply to
The basic idea may be something like this...

Code:
my @slogans = ('This is slogan one', 'This is slogan two');
my $inputted_word = 'is';
my @matches = grep /\b\Q$inputted_word\E\b/i, @slogans;

print $matches[rand @matches];

That looks for all slogans that contain "is" and then prints a random one.

You can obviously change it to read from a file if you want.

Last edited by:

Paul: Feb 27, 2003, 4:46 AM
Quote Reply
Re: [Curt] CGI Script writing challenge In reply to
I have a script on my site that is for the old "Mad Libs"
type game where you put in a word and it puts it in the
story for you.

That may be able to be adapted directly.

The script is by Darryl Burgdorf

http://awsd.com/scripts/

Here's the main part of it.
_____________________________



$InputFile = "/mysite.com/cgi-bin/funnyads/funnyads.htm";
$TextFile = "/mysite.com/cgi-bin/funnyads/text";
$CGIURL = "http://mysite.com/...funnyads/funnyads.pl";

print "Content-type: text/html\n\n";

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs){
($name, $value) = split(/=/, $pair);
$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
if ($FORM{$name}) {
$FORM{$name} = "$FORM{$name}, $value";
}
else {
$FORM{$name} = $value;
push (@variables,$name);
}
}

if ($FORM{'TextFile'}) { &SendFile; }

if (-d $TextFile) {
opendir(FILES,$TextFile);
@tempfiles = readdir(FILES);
closedir(FILES);
foreach $file (@tempfiles) {
if (-T "$TextFile/$file") {
push (@files, $file);
}
}
srand();
$files = @files;
$file = @files[int(rand($files))];
$TextFile = $TextFile."/".$file;
}

open (FILE,"$TextFile");
@LINES = <FILE>;
close (FILE);
$text = join(' ',@LINES);
$text =~ s/\n/ /g;
$text =~ s/</\n</g;
$text =~ s/>/>\n/g;
@text = split('\n',$text);

undef (@variables);

foreach $line (@text) {
if ($line =~ s/<!--(.*)-->/$1/) {
push (@variables,$line);
}
}

@sortedvariables = sort (@variables);

open (FILE,"$InputFile");
@LINES = <FILE>;
close (FILE);

foreach $line (@LINES) {
if ($line =~ /<!--InputWords-->/i) {
print "<FORM METHOD=POST ACTION=\"$CGIURL\">\n";
print "<INPUT TYPE=HIDDEN NAME=TextFile ";
print "VALUE=\"$TextFile\">\n";
print "<P><CENTER><TABLE>\n";
foreach $variable (@sortedvariables) {
next if ($variable eq $lastvariable);
print "<TR><TD ALIGN=RIGHT><P>$variable: </TD>";
print "<TD><INPUT TYPE=TEXT NAME=\"$variable\" SIZE=25>";
print "</TD></TR>\n";
$lastvariable = $variable;
}
print "</TABLE></P>\n";
print "<P><INPUT TYPE=SUBMIT VALUE=\"Create Funny Ads\">\n";
print "</CENTER></P></FORM>\n";
}
else {
print $line;
}
}

exit;

sub SendFile {
open (FILE,"$FORM{'TextFile'}");
@LINES = <FILE>;
close (FILE);
$text = join(' ',@LINES);
$text =~ s/\n/ /g;
foreach $variable (@variables) {
if ($FORM{$variable} eq "") {
print "incomplete!\n";
exit;
}
$text =~ s/<!--$variable-->/<STRONG>$FORM{$variable}<\/STRONG>/g;
}
$text =~ s/A\/an <STRONG>(a|e|i|o|u)/An <STRONG>$1/g;
$text =~ s/a\/an <STRONG>(a|e|i|o|u)/an <STRONG>$1/g;
$text =~ s/A\/an/A/g;
$text =~ s/a\/an/a/g;
print $text;
exit;
}




_____________________________


In the HTML page you simply insert:

<!--InputWords-->


_____________________________

Then in the cgi-bin/funnyads/text directory you put as many
.txt files as you want that each look something like:

Nike--Just <!--Verb--> it!

or


General Electric--We Bring <!--Adjective--> Things to Life


_____________________________

One problem with the script is that it randomly selects the
.txt file from the cgi-bin/funnyads/text directory but it
does not check to see if that .txt file has been used before
so you might get the same one whenever you refresh the
page.

_____________________________

You can take a look at the script in action on my site:

http://churchfun.com/...nt_announcements.htm


_____________________________

Really, if there was just a way to make sure that the files
don't repeat themselves this would be perfect for the slogan
script.



Now, I don't pretend to know much PERL. I can cut and paste
and set permissions and MAYBE change a variable or two.

I guess I know just enough to be picky about how the script
works.

If there is any way to modify the script or if you can think
of a better way to do this it would be great.

Thank you for looking at this.
Quote Reply
Re: [Curt] CGI Script writing challenge In reply to
Okay here is my attempt at this script. I adapted the script above. See it in action.

http://churchfun.com/...sillyads/sillyads.pl

But, I still need a little help. The script takes a text file that I have created with the ads and variables, processes the file and returns the text string with the variable inserted. (Visit the link and it will make sense.) It does this randomly. However, the script does not do any checking to see if the file has been used recently.

So, it is possible to get the same ad over again or see the same one every third or fourth time you use the script.

I don't want it to simply take the text files in order because it would always start with the same ones and repeat visitors will have already seen those files.

Is there anyway to keep it random but also keep it from repeating for the user during the same session?