bobbyboy has asked for the wisdom of the Perl Monks concerning the following question:

I have managed to get this search engine to work on my site but when you type in a keyword which does not appear within the site you just get a blank screen response. I want to give a response of Sorry was unable to match you response or something along those lines. It would be great if someone could help me solve this problem. Here is the code below.

Thanks

bobbyboy

#!/usr/bin/perl5 require "get_form_data.pl"; &get_form_data(); $search_term = $FORM{'search'}; print "Content-type: text/html\n\n"; opendir(DIR, "."); while($file = readdir(DIR)) { next if($file !~ /.html/); open(FILE, $file); $found_match = 0; $title = ""; while(<FILE>) { if(/$search_term/i) { $found_match = 1; } if((/<TITLE>/) || ($found_title)) { if((/<\/TITLE>/) && (/<TITLE>/)) { chop; $title = $_; $title =~ s/<TITLE>//g; $title =~ s/<\/TITLE>//g; } else { if($found_title == 1) { $title = $_; $found_title = 2 } elsif($found_title == 2) { $found_title = 0; } else { $found_title = 1; } } } } if($found_match) { print "<A HREF=\"$file\">$title</A>\n"; print "<BR>\n"; } close(FILE); } closedir(DIR); exit;

Edited 2001-04-03 by mirod: added <code> tags

Replies are listed 'Best First'.
Re: Sorry message
by c-era (Curate) on Apr 03, 2001 at 16:08 UTC
    Make sure you use the <code> tages, then your code will look like this:
    #!/usr/bin/perl5 require "get_form_data.pl"; &get_form_data(); $search_term = $FORM{'search'}; print "Content-type: text/html\n\n"; opendir(DIR, "."); while($file = readdir(DIR)) { next if($file !~ /.html/); open(FILE, $file); $found_match = 0; $title = ""; while() { if(/$search_term/i) { $found_match = 1; } if((//) || ($found_title)) { if((/<\/TITLE>/) && (//)) { chop; $title = $_; $title =~ s///g; $title =~ s/<\/TITLE>//g; } else { if($found_title == 1) { $title = $_; $found_title = 2 } elsif($found_title == 2) { $found_title = 0; } else { $found_title = 1; } } } } if($found_match) { print "<A HREF=\"$file\">$title</A>\n"; print "<BR>\n"; } close(FILE); } closedir(DIR); exit;
    An easy way to do what you want is to add another variable, and if a match is found put a 1 in it. At the end of your script if the variable == 0 then print your sorry message.
Re: Sorry message
by cLive ;-) (Prior) on Apr 03, 2001 at 18:14 UTC
    Amending your code:
    #!/usr/bin/perl5 require "get_form_data.pl"; &get_form_data(); $search_term = $FORM{'search'}; my @match; opendir(DIR, "."); while($file = readdir(DIR)) { next if($file !~ /.html/); open(FILE, $file); $found_match = 0; $title = ""; while(<FILE>) { if(/$search_term/i) { $found_match = 1; } if((/<TITLE>/) || ($found_title)) { if((/<\/TITLE>/) && (/<TITLE>/)) { chop; $title = $_; $title =~ s/<TITLE>//g; $title =~ s/<\/TITLE>//g; } else { if($found_title == 1) { $title = $_; $found_title = 2 } elsif($found_title == 2) { $found_title = 0; } else { $found_title = 1; } } } } if($found_match) { push @match, qq(<A HREF="$file">$title</A><BR>\n); } close(FILE); } closedir(DIR); # now output print "Content-type: text/html\n\n"; if (@match) { for (@match) { print; } } else { print "Sorry, no matches found"; } exit;
    I'm resisting doing more than you need to this, but I also think you should:
    • use strict; (ie declare all variables)
    • use qq(interpolated text) quotes so you don't have to escape \" chars (see above) and, similarly, change your delimiter on your match (this will mean you must start it with 'm' though - see my example below)
    • slurp file in (see below)
    And look at these lines and see if they click?
    ... open(FILE, $file); $file_contents = join '', (<FILE>); close (FILE); $file_contents =~ m|<title>(.*?)</title>|is; my $title = $1; if ($file_contents =~ m|$search_term|is) { push @match, qq(<A HREF="$file">$title</A><BR>\n); }
    The 's' modifier basically says "search the whole page in one go rather than line by line". The 'i' modifier says ignore case (title == TITLE == TiTLe etc)

    If you start the match with an m, you can change the delimiter, making it easier to read (Ie you don't have to escape /). Similarly for s|one|two|is;

    cLive ;-)

      Thanks for your help and input bobbyboy
Re: Sorry message
by sutch (Curate) on Apr 03, 2001 at 16:10 UTC
    In the future, you may want to use some HTML along with <code> tags to format the content of your question.

    You need a variable to track whether a match was found. I recommend setting it to 0 before the while loop, then incrementing it for each match. After the while loop, if the variable is false (still equal to 0) then print your "not found" message. This technique has the added benefit of providing you with the number of matches.