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:
else problems

 



perlkid
stranger

Feb 21, 2000, 1:19 AM

Post #1 of 13 (2045 views)
else problems Can't Post

 
Hi everyone,

I am having the most trouble I have had yet. I am trying to do a simple else statement in a small search engine. Like so:

------------------------------------------------------------------------
if ($input =~ perl)
{
open(FILE,"/path/to/cgi-bin/bin/data.db");
@data=<FILE>;
close(FILE);
foreach $line(@data) {
@words=split(/\|/,$line);
if ($words[0] eq "$in{'qry'}") ##the form name's input
{
print "$words[1]";
}
else
{
print $noresults;
}
} ##end of foreach

} ##end of first if
-------------------------------------------------------------
I just put the ##statements in there for me to keep track while I tried to put the else statement in different places.

It's printing no results for everything I search for (and I know that they are in the data base) except for what is in the first line. What else can I do?

Thanks a lot,

perlkid




brian.hayes
User

Feb 21, 2000, 4:22 AM

Post #2 of 13 (2045 views)
Re: else problems [In reply to] Can't Post

When I have problems simalar to this I use the good ole print statment.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


if ($input =~ perl){
print "<p>First if </p>\n";

open(FILE,"/path/to/cgi-bin/bin/data.db");
@data=<FILE>;
close(FILE);

print "<p>Data is in</p>\n";

foreach $lines(@data) {
@words=split(/\|/,$lines);
if ($words[0] eq $in{'qry'}) ##the form name's input
{
print $words[1];
}
else
{
## this will let you see the error..
print "<p> No Match for", $words[0], "to", $in{'qry'}, "</p>\n";

print $noresults;
}
} ##end of foreach

} ##end of first if

</pre><HR></BLOCKQUOTE>

No much help really, but will allow you to trouble shoot the situation.

Also you have "$in{'qry'}" should be without the quotes. This is stringification which you do not want. Perl will see this for face value "the string $in{'qry'}" instead of the actual value "$in{'qry'} actually = testing". This may be your problem as well.

Let me know.
Brian Hayes



Cure
User

Feb 21, 2000, 9:50 AM

Post #3 of 13 (2045 views)
Re: else problems [In reply to] Can't Post

Hi perlkid

Here you go:->

if ($input =~ perl)
{
open(FILE,"/path/to/cgi-bin/bin/data.db");
@data=<FILE>;
close(FILE);
foreach $line(@data) {
chomp($line);
@words=split(/\|/,$line);
if (@cure =grep /$in{'qry'}/,@words) ##the form name's input
{
print "@cure";

}
else
{
print $noresults;
}
} ##end of foreach

Cure


perlkid
stranger

Feb 22, 2000, 6:56 PM

Post #4 of 13 (2045 views)
Re: else problems [In reply to] Can't Post

 Hi Cure, Hi Brian,

I'm very appreciative of the responses. I have been trying to get this to work all day but even with your code's I was unsuccessful. I was trying to make my original post short and simple so people would not be confused but I think it would be better if I said exactly my problem and what I'm trying to do.

Here it goes,

I have a data base called afkey.db and I have a small code that reads the database and matches any word with the form's input. It is a simple search engine. And when it finds a match in that database I want it to print location to search engine (A) with that key word as the query. I have been able to do that, but when I want to add an else statement so that when there is no match in the data base it will go to search engine (B) but it will go there all the time, regardless of the keywords in the file. With the else statement, the only time it will go to search engine (A) is when I type in what's in the first line. Other wise it always goes to search engine (B). I know that Iím typing in the right keywords, I mean; I open up the database and there right there. It's supposed to be like a redirect based on the keywords in a file (afkey.db). Well now that you know what I am trying to accomplish, here is the code.

______________________________________________________________________


%in = &parse_form;

open(FILE,"</path/to/cgi-bin/database/afkey.db");
@data=<FILE>;
close(FILE);
foreach $line(@data) {
@words=split(/\|/,$line);
$words[0]=lc($words[0]);
if ($words[0] eq "$in{'qry'}")
{
print "location:http://www.mysite.com/cgi-bin/redirect.pl?search=$words[0]&whereto=test&perpage=10\n\n";
}
else
{
print "location:http://www.mysite.com/cgi-bin/redirect.pl?search=$in{'qry'}&whereto=search2&perpage=10\n\n";
}
}

sub parse_form {
my (@pairs, %in);
my ($buffer, $pair, $name, $value);

if ($ENV{'REQUEST_METHOD'} eq 'GET') {
@pairs = split(/&/, $ENV{'QUERY_STRING'});
}
elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
}
else {
&cgierr('You cant run this script from telnet/shell.');
}

PAIR: 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;

($value eq "---") and next PAIR;
exists $in{$name} ? ($in{$name} .= "~~$value") : ($in{$name} = $value);
}
return %in;
}


______________________________________________________________________

My main problem is that with that code, the only time it goes to the test engine (search engine A) is when I type in the keyword in the first line of the database.


Thank you guys so much,

perlkid

[This message has been edited by perlkid (edited 02-22-2000).]


brian.hayes
User

Feb 22, 2000, 7:27 PM

Post #5 of 13 (2045 views)
Re: else problems [In reply to] Can't Post

I see, try this.

Change this:
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


if ($words[0] eq "$in{'qry'}")
{
print "location:http://www.mysite.com/cgi-bin/redirect.pl?
search=$words[0]&whereto=test&perpage=10\n\n";
}
else
{
print "location:http://www.mysite.com/cgi-bin/redirect.pl?
search=$in{'qry'}&whereto=search2&perpage=10\n\n";
}
}
</pre><HR></BLOCKQUOTE>

to this:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


#!/usr/bin/perl

%in = &parse_form;

open(FILE,"</path/to/cgi-bin/database/afkey.db");
while(<FILE> ){
@words=split(/\|/,$_);
$words[0]=lc($words[0]);

if($in{'qry'} =~ /$words[0]/){
print "location:http://www.mysite.com
/cgi-bin/redirect.pl?
search=$words[0]&whereto=test&perpage=10\n\n";
} elsif ($in{'qry'} !~ /$words[0]/){
print "location:http://www.mysite.com
/cgi-bin/redirect.pl?
search=$in{'qry'}&whereto=search2&perpage=10\n\n";
} else {
print "<p>Error locating" ,
$in{'qry'} , "in your search.</p>\n";
}

close(FILE);


sub parse_form {
my (@pairs, %in);
my ($buffer, $pair, $name, $value);

if ($ENV{'REQUEST_METHOD'} eq 'GET') {
@pairs = split(/&/, $ENV{'QUERY_STRING'});
}
elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
}
else {
&cgierr('You cant run this script from telnet/shell.');
}

PAIR: 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;

($value eq "---") and next PAIR;
exists $in{$name} ? ($in{$name} .= "~~$value") : ($in{$name} = $value);
}
return %in;
}
</pre><HR></BLOCKQUOTE>

just check for typos..Let me know if I made any mistakes here...

Brian Hayes

[This message has been edited by brian.hayes (edited 02-23-2000).]


brian.hayes
User

Feb 22, 2000, 7:30 PM

Post #6 of 13 (2045 views)
Re: else problems [In reply to] Can't Post

Forgot to close the while statment. Put a } at the end of the } in the if, elsif , else statment. Just above the close(FILE); statment.

Brian Hayes


perlkid
stranger

Feb 22, 2000, 7:54 PM

Post #7 of 13 (2045 views)
Re: else problems [In reply to] Can't Post

 
Thanks for the quick response. I'm going to go try that right now and I'll let you know what happends.

Thanks Brian,

perlkid


perlkid
stranger

Feb 22, 2000, 9:27 PM

Post #8 of 13 (2045 views)
Re: else problems [In reply to] Can't Post

 Brian,

I tried that out but It only works when the $in{'qry'} is wrong.

____________________________________________________

%in = &parse_form;

open(FILE,"</path/to/cgi-bin/database/afkey.db");
while(<FILE> )
{
@words=split(/\|/,$_);
$words[0]=lc($words[0]);
if($in{'qry'} =~ /$words[0]/)
{
print "location:http://www.mysite.com/cgi-bin/redirect.pl?qry=$words[1]&whereto=test&perpage=10\n\n";
}
elsif ($in{'qry'} !~ /$words[0]/)
{
print "http://www.mysite.com/cgi-bin/redirect.pl?qry$in{'qry'}\n\n";
}
}
close(FILE);

_____________________________________________________

When there right I get an internal server error.

Do you see what I mean?

Thanks,

perlkid

[This message has been edited by perlkid (edited 02-22-2000).]


brian.hayes
User

Feb 23, 2000, 3:36 AM

Post #9 of 13 (2045 views)
Re: else problems [In reply to] Can't Post

Try removing the print line that you have with something simple like:
if(statement){
print "Content-Type: text/html\n\n";
print "It worked" , $in{'qry'} , $words[0] , "\n";
} elsif (statement) {

}

This will at least get you to the if without error.

Plus I would keep the last else statment in this for all the unknowns.

Finally: I allways try to split the print statments up so that there are no "" around any variables.


print "location:http://www.mysite.com/cgi-bin/redirect.pl?qry=" , $words[1] , "&whereto=test&perpage=10" ,"\n\n";

Brian Hayes


[This message has been edited by brian.hayes (edited 02-23-2000).]


brian.hayes
User

Feb 23, 2000, 3:39 AM

Post #10 of 13 (2045 views)
Re: else problems [In reply to] Can't Post

Don't forget to look at you error log file. WHen you get server errors like this it will write to the error log. Usually it will tell you what line the file failed.

Brian Hayes


brian.hayes
User

Feb 23, 2000, 3:47 AM

Post #11 of 13 (2045 views)
Re: else problems [In reply to] Can't Post

add this to the top of your script it may help you find errors as well.


#/path/to/perl

use CGI::Carp qw(fatalsToBrowser);


perlkid
stranger

Feb 23, 2000, 6:26 PM

Post #12 of 13 (2045 views)
Re: else problems [In reply to] Can't Post

 
Thanks Brian,

I got it to work!

Thanks a lot!

perlkid


brian.hayes
User

Feb 23, 2000, 6:48 PM

Post #13 of 13 (2045 views)
Re: else problems [In reply to] Can't Post

No problem...Glad we could help out.

Brian Hayes

 
 


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

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