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: Beginner:
2 on 1

 



JC
Novice

May 28, 2000, 3:29 PM

Post #1 of 16 (3658 views)
2 on 1 Can't Post

Hello All,

Got this script driving me nuts. It's a take off of Ask Jevees an NT VBasic script. This script is Perl. There's four functions, search, search results drop down list, answers page and error message. When I first set up the script everything worked except on the answers page it printed Content-type: text/html\n\n at the top left and preased both the answers page [as it should] but also the error page [it should not]. I fixed the Content-type: text/html\n\n prob. I found it had been entered two times in the script. removed them both from where they had been entered and added it once to the beging of the script. But for the life of me I haven't been able to figure out why it preases tow pages as stated above. Could it be a server set up prob. The url to this is [new site under construction] http://theteacabinet.com form location right of graphic page title. Any ideas will be greatly apprc.

TIA Smile

Oh ... the script can be found at http://www.slack.net/~pete/perl ... AskStu


perlkid
stranger

May 29, 2000, 3:07 AM

Post #2 of 16 (3658 views)
Re: 2 on 1 [In reply to] Can't Post

 
I know why, But You'll have to paste the code if anyone is going to help you. It's probably an if then statement that's not taking into consideration that there is an answer and an error message is not needed.

perlkid


JC
Novice

May 29, 2000, 4:28 AM

Post #3 of 16 (3658 views)
Re: 2 on 1 [In reply to] Can't Post

Yep figure as much just don't see where, thanks.
here's the code ...

#!/usr/bin/perl

### Based on Compu-Stu search script copyright copyright Team 25018 of
### Thinkquest 99 (of which Pete was a member)

### Pete's AskStu - by Pete Stein (pete@slack.net) - 11-2-99
### http://www.slack.net/~pete/perl
###
### This script copyright Pete Stein, pete@slack.net from Pete's Perl Scripts
### at http://www.slack.net. Users of this script acknowledge the following
### terms and conditions. This script is provided free of charge and as a
### result is Freeware, but still owned and copyright Pete Stein. Users may
### modify the script but may not in any circumstance redistribute or sell
### this script in any form or fashion without express consent of Pete Stein
### Users are not obligated to provide any acknowledgement of the origin of
### this script if used on their site, but they certainly may if they so
### desire.

### All images contained in this distribution, stubox.jpg and stu.jpg
### follow the same aforementioned conditions

## base directory, like /home/pete
## no trailing slash

$basedir = '/home/tea';

## name of the directory where answer files are stored
## no trailing slash

$studir = 'teafiles';

## baseurl, like http://www.slack.net/~pete, wherever stu.jpg will be stored
## no trailing slash

$baseurl = 'http://theteacabinet.com/images';

## location of this script

$thiscgi = 'http://theteacabinet.com/cgi-bin/Ask-Stu/askstu.cgi';

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

# (post method)

# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

# Split the name-value pairs
@pairs = split(/&/, $buffer);

foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);

$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

$FORM{$name} = $value;
}


sub doanswer {

$file = $FORM{'answer'};

open(FILE,"$basedir/$studir/$file");
@lines = <FILE>;
close(FILE);

$string = join(' ',@lines);
$string =~ s/\n//g;

if ($string =~ /<answer>(.*)<\/answer>/i) {
@answer = "$1";
}

if ($string =~ /<title>(.*)<\/title>/i) {
$title = "$1";
}

print "
<html>
<head>
<title>I know that!</title></head>
<body>
<center>
<table border=0 cellspacing=0 cellpadding=0 width=500>
<tr><td>
<img src=\"$baseurl/teabutton.gif\">
</td>
<td width=10>
<br>
</td>
<td>
<b>
<font face=\"Arial, Verdana, Helvetica\" size=\"3\">
<center>$title</center></font></b><p><br>
<font face=\"Arial, Verdana, Helvetica\" size=\"2\">
@answer
</font>
<p>
<center>
<font face=\"Arial, Verdana, Helvetica\" size=\"2\">
<b><a href=\"Javascript:history.back(); history.back();\"><<<< Return</a><p>
<a href=\"Javascript:history.back();\"><<< Back</a></b>
</center>
</font>
<P>";
# you can remove this blurb if you want.... Frown
# don't feel guilty
# really
# it's nooo problem
# *sniff*
print "<font size=1>This script provided by <a href=\"http://www.slack.net/~pete/perl\">Pete's Perl
Scripts</a><P> Compu-Stu is from <a href=\"http://library.advanced.org/25018\">UCS: The Ultimate Computer
Source</a>, a <a href=\"http://www.thinkquest.org\">Thinkquest</a> entry. Compu-Stu provided interactive
help to visitors of the site. We welcome you to visit it
</td></tr></table>
<p><br>
</body>
</html>

";

}


#Get the files here for later...
@files = ('*.stu');
&get_files;
$num=0;
#Split the user's question by spaces
$daquest = $FORM{'question'};

if ($daquest =~ /\?/) {

chop($daquest);

}

@terms = split(/\s+/, $daquest);

$i=0;

if ($FORM{'step'} eq "answer") {
&doanswer;
}

foreach $FILE (@FILES) {
#Loop through each file and search for keywords
$i++;
$ismatch=0;

#Open file here and store it in @lines
open(FILE,"$FILE");
@lines = <FILE>;
close(FILE);

$string = join(' ',@lines);
$string =~ s/\n//g;

if ($string =~ /<keywords>(.*)<\/keywords>/i) {
$keywords = "$1";
}
#split the keywords up too
@words = split(/\s+/, $keywords);
foreach $word (@words) {
#loop through each word and compare it to every keyword
foreach $term (@terms) {
if ("\L$term\E" EQ "\L$word\E") {
$ismatch=1;
}
}
}

#Now ismatch should be 1 if something is found and 0 if it wasn't
#Now we have to add this file to an array somehow
if ($ismatch==1) {
$stus{$i}="yes";
$nme{$i}=$FILE;
$num++;
}

if ($string =~ /<title>(.*)<\/title>/i) {
$question{$i} = "$1";
}

}


#Loop here to print out the code for the questions and the answers they can recieve
$k=0;

if ($num>0) {
print "

<html>
<head>
<title>I think I know the answer!</title>
</head>
<body>
<p>
<center>
<table border=0 cellspacing=0 cellpadding=0 width=500>
<tr><td valign=\"top\">
<img src=\"$baseurl/answlist.gif\" align=\"left\" hspace=\"5\"><b>
<font face=\"Arial, Verdana, Helvetica\" size=\"3\">
<center>We think we have the Answer!</center></font></b><p>
<div align=\"justify\">We searched our knowledge database and we matched your question with some of the potential questions we can
answer. We made a list for you. Use the pulldown box below to locate the question you're looking for. Click answer, and you've got
it!</div><P>
<form method=\"post\" action=\"$thiscgi\">
<input type=hidden name=step value=answer>

<select name=\"answer\">";
for ($j=1; $j<=$i; $j++) {
if($stus{$j} EQ "yes") {
print "<option value=$nme{$j}>$question{$j}</option>";
}
}


print "</select>

<input type=\"submit\" value=\"Answer\">
</form>
<center>
<font face=\"Arial, Verdana, Helvetica\" size=\"2\">
<b><a href=\"Javascript:history.back();\"><< Back</a></b>
</font>
</center>
<P>";
# you can remove this blurb if you want.... Frown
# don't feel guilty
# really
# it's nooo problem
# *sniff*
print "
<font size=1>This script provided by <a href=\"http://www.slack.net/~pete/perl\">Pete's Perl
Scripts</a><P> Compu-Stu is from <a href=\"http://library.advanced.org/25018\">UCS: The Ultimate Computer
Source</a>, a <a href=\"http://www.thinkquest.org\">Thinkquest</a> entry. Compu-Stu provided interactive
help to visitors of the site. We welcome you to visit it.
</td></tr></table>

";

}

if ($num==0) {
print "

<html>
<head><title>Sorry...</title></head>
<body>

<center>
<table border=0 cellspacing=0 cellpadding=0 width=500>
<tr><td>
<img src=\"stu.jpg\" align=\"right\"><b>
<center><b>I am sorry, I don't have an answer to that.</b></center><font size=2><p>
</b>
I'm sorry, I do not know the answer to your question. Maybe you can find it elsewhere in the site, or
use different words in your question?<P>

</font><p></td></tr></table>

<font face=\"Arial, Verdana, Helvetica\" size=\"2\">
<b><a href=\"Javascript:history.back();\"><< Back</a></b>
</font>

</center>

</body>
</html>

";

}

sub get_files {

chdir("$basedir/$studir");
foreach $file (@files) {
$ls = `ls $file`;
@ls = split(/\s+/,$ls);
foreach $temp_file (@ls) {
if (-d $file) {
$filename = "$file$temp_file";
if (-T $filename) {
push(@FILES,$filename);
}
}
elsif (-T $temp_file) {
push(@FILES,$temp_file);
}
}
}
}

Thanks
JC

[This message has been edited by JC (edited 05-29-2000).]


perlkid
stranger

May 29, 2000, 11:10 AM

Post #4 of 16 (3658 views)
Re: 2 on 1 [In reply to] Can't Post

 Not exactly what I expected,

But try replacing

if ($num==0) {

with

if ($num<1) {

and if that doesn't work try taking that line out and replacing it with else {

I'm referring to the line above the part where it prints the error message.

perlkid

[This message has been edited by perlkid (edited 05-29-2000).]


perlkid
stranger

May 29, 2000, 11:43 AM

Post #5 of 16 (3658 views)
Re: 2 on 1 [In reply to] Can't Post

 
I made a mistake, You can't use the else statement there, You'll get an internal server error.

Let me know if

if ($num<1){

works for you

perlkid


JC
Novice

May 29, 2000, 11:50 AM

Post #6 of 16 (3658 views)
Re: 2 on 1 [In reply to] Can't Post

Thanks for the try but else kills the script and if ($num<0) { kills the error message completely .. ie document contains no data but it runs the other two funtions ok ... this bugger is driving me nuts. I contacted the auther and he say's it works on his servers .. and it does even with the two rint "Content-type: text/html\n\n"; that I changed as noted above ... wired !

JC


JC
Novice

May 29, 2000, 11:54 AM

Post #7 of 16 (3658 views)
Re: 2 on 1 [In reply to] Can't Post

no if ($num<1){ produces the same results as if ($num==0){

JC Wink


Cure
User

May 29, 2000, 12:28 PM

Post #8 of 16 (3658 views)
Re: 2 on 1 [In reply to] Can't Post

if ($num > 1) IS NOT THE SAME AS
if ($num==0)


JC
Novice

May 29, 2000, 1:03 PM

Post #9 of 16 (3658 views)
Re: 2 on 1 [In reply to] Can't Post

Yes Cure, all the suggestions ($num > 1) or if ($num < 1) kills the error message from working at all ... so i figured they were not the answer. I tried to play with this array ... and got different results but still not right ...

#Now ismatch should be 1 if something is found and 0 if it wasn't
#Now we have to add this file to an array somehow
if ($ismatch==1) {
$stus{$i}="yes";
$nme{$i}=$FILE;
$num++;
}

I appreciate everyones input ... even that which is wrog or doesn't work helps me along and is educational ... Wink

JC

P.S. Thanks perlguru.com for this Great forum

[This message has been edited by JC (edited 05-29-2000).]


perlkid
stranger

May 29, 2000, 2:10 PM

Post #10 of 16 (3658 views)
Re: 2 on 1 [In reply to] Can't Post

 If I understand Correctly

for ($j=1; $j<=$i; $j++) {
if($stus{$j} EQ "yes") {
print "<option value=$nme{$j}>$question{$j}</option>";
}
}

html

}

is where to results are produced.

Why don't you try putting a scalar with a value inside

if($stus{$j} EQ "yes") {
$printerror = "no";

Then instead of using the if($num==0){

try

unless($printerror eq "no"){

Let Me Know If That Works.

Sorry For The bad input earlier.. Frown

perlkid



[This message has been edited by perlkid (edited 05-29-2000).]


JC
Novice

May 29, 2000, 3:18 PM

Post #11 of 16 (3658 views)
Re: 2 on 1 [In reply to] Can't Post

well I gave this a try ...

for ($j=1; $j<=$i; $j++) {
if($stus{$j} EQ "yes") {
$printerror = "no";
print "<option value=$nme{$j}>$question{$j}</option>";

and change to .. unless($printerror eq "no"){
at the error page .. no change, same effect

Thanks for trying
JC


perlkid
stranger

May 30, 2000, 12:32 AM

Post #12 of 16 (3658 views)
Re: 2 on 1 [In reply to] Can't Post

 Your problem is now haugnting me. I'm sorry I couldn't help you. If I was on your server I'm sure I could though. But what you should do is let the guy who programmed it know that he should be the one on your server fixing the problem. I can't believe that he would let something go like that after you told him about it. "Well I works on my server" he says, of course it does. But why sell a script that your not sure will work on all unix based os's.(Just My Opinion Smile)

Good Luck To Ya,

perlkid

[This message has been edited by perlkid (edited 05-30-2000).]


JC
Novice

May 30, 2000, 6:33 AM

Post #13 of 16 (3658 views)
Re: 2 on 1 [In reply to] Can't Post

Well it was a free script and you can't ask to much for free. I myself would want to figure it out had I wrote it but that's me.

I'll figure it out eventually .. or write my own someday Wink

Thanks Again
JC

P.S. When I fix it I'll let you know what it was. Don't lose sleep over it Smile


perlkid
stranger

May 30, 2000, 10:01 AM

Post #14 of 16 (3658 views)
Re: 2 on 1 [In reply to] Can't Post

  

Are you still talking about the ask the tea guys script on your site. That's been the one right. Because I just went there and it was working. Did you get it sorted out?

perlkid Smile


JC
Novice

May 30, 2000, 10:30 AM

Post #15 of 16 (3658 views)
Re: 2 on 1 [In reply to] Can't Post

Hi perlkid,

naw I didn't get it fixed but I steped around the problem through design, the error page still comes up on the answers page you just can't see it. I set up the script to open in a remote window and <p><br> about 50 times below the </table> tage on the answers page so even it the answer is long enought to need a scroll bar you won't see it .. lol unless you just like to scroll a page for no reason.
Just have the error page to fix up inside the script to match like the rest and it'll be done. lol except the database.

I hope to solve the problem eventually but I've been tring now for 4 days and got to get this site further along. Been unable to find another script like it. I thought of converting a search eng over but decided to hold of on that idea.

Thanks for your help ... Wink

JC


perlkid
stranger

May 30, 2000, 9:03 PM

Post #16 of 16 (3658 views)
Re: 2 on 1 [In reply to] Can't Post

 
Thanks For Keeping Me Posted.

Good Luck,

perlkid Smile

 
 


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

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