search - no matches in a text file

I edited a script to search through a text file and display the output in web page(HTML).

It finds the results and displays it properly.
However,how do I check for no matching records and/or an empty query. Then display a message indicating this and give the user the option to try again (the result ofcourse also to be displayed in a web page)?

Any ideas, please?

The script:
---------------------------
#!/usr/bin/perl
print "Content-Type: text/html\n\n";

# what does the user want to find?
($dump,$search) = split(/=/,$ENV{'QUERY_STRING'});

# search not case-sensitive
$ucsearch = uc($search);

&parseInput;

open(THE_FILE, "email.txt");

while (<THE_FILE>)
      {
@elements = split(/::/);
$record = $_;
$_ = uc($_);
if (/$ucsearch/)
{
&output(@elements);
exit;
}
}

#-------------------------------Output HTML-----------------------------------------#

sub output($)
{

local(@elements) = @_;

print <<"EOF";

<HTML>
<HEAD>
<TITLE> New Document </TITLE>

</HEAD>

<BODY BGCOLOR="#FFFFFF">

<table width=430 border=0>
<tr><td>Name</td><td></td><td>$elements[1]<br></td></tr>
<tr><td>Email</td><td></td><td>$elements[2]<br></td></tr>  
<tr><td>Ext.</td><td></td><td>$elements[3]<br></td></tr>
<tr><td>Division</td><td></td><td>$elements[4]<br></td></tr>
</table>
<p></p>


</BODY>
</HTML>
EOF

}

#---------------------------------Parse Input---------------------------------------#
# Arguments: NONE #
# Purpose Standard Form/Query String Parsing, places values in the #
# %fields #
# Calls: NONE #
#-----------------------------------------------------------------------------------#

sub parseInput()
{
#Parse form variables
if ($ENV{'REQUEST_METHOD'} eq "POST")
{
read(STDIN, $temp, $ENV{'CONTENT_LENGTH'});
}

elsif ($ENV{'REQUEST_METHOD'} eq "GET" )
{
$temp = $ENV{'QUERY_STRING'};
}

@pairs=split(/&/,$temp);

foreach $item(@pairs)
{
($key,$content)=split (/=/,$item,2);
$content=~tr/+/ /;
$content=~ s/%(..)/pack("c",hex($1))/ge;
$fields{$key}=$content;
}

}

##########################
ggdAsked:
Who is Participating?
 
maneshrConnect With a Mentor Commented:
print "Content-Type: text/html\n\n";

# what does the user want to find?
($dump,$search) = split(/=/,$ENV{'QUERY_STRING'});

# search not case-sensitive
$ucsearch = uc($search);

&parseInput;

open(THE_FILE, "email.txt");

$flag_found=0;

while (<THE_FILE>)
{
@elements = split(/::/);
$record = $_;
$_ = uc($_);
if (/$ucsearch/)
{
&output(@elements);
$flag_found++;  ## increment flag to indicate a match was found
last;  ## come out of the while loop
}
}


if (!$flag_found){  ## no match found since var is 0
  print "No match found\n";
}


========================

You can check for empty values using the foll

Lets say you have read the form variable into your PERL variable called $search.


if (length($search)<=0 || $search=~ /^\s+$/){  ## empty value or spaces sent

  print "Some error message\n";
  exit;
}

0
 
MindoCommented:
The script:
                         ---------------------------
                         #!/usr/bin/perl
                         print "Content-Type: text/html\n\n";

                         # what does the user want to find?
                         ($dump,$search) = split(/=/,$ENV{'QUERY_STRING'});

if(length($search) > 0)
{
  print "We have a search query.\n";
}
else
{
  print "The search query is empty.\n";
}

                         # search not case-sensitive
                         $ucsearch = uc($search);

                         &parseInput;

                         open(THE_FILE, "email.txt");
$found = 0;

                         while (<THE_FILE>)
                         {
                         @elements = split(/::/);
                         $record = $_;
                         $_ = uc($_);
                         if (/$ucsearch/)
                         {
                         &output(@elements);
$found = 1;
                         exit;
                         }
                         }

if($found)
{
  print "There were records.\n";
}
else
{
  print "There were no records.\n";
}

=======================================
To find out if you have a search query just check the length of your $search string.

To find out if your query yelded results have a variable $found and set it to 1 if you found results. If there were not matching records the $found variable is equal to 0.
0
 
ggdAuthor Commented:
ok I have looked at the options but am not sure it will result in what I was trying to achieve.

Currently the script checks for the string in the text file and displays a formated result in a web page (like above).

Now I want to check:
1. If the query is empty
Result : Generate an "web/html" page with the content of the web page to display the message "the Query was empty". And Give the user the option to try again.
2.If the query did not find any matches in the text file.
Result: Generate an "web/html" page with the content of the web page to display the message "No matches were found". And Give the user the option to try again.


0
 
maneshrCommented:
try this....

=============homepage.txt
                [Home]                     [MSN]                   [Image]


[Image][Image][Image][Image]              [Image][Image][Image][Image][Image]
[Image][Image][Image]SEARCH the web[Image]       [Image]       [Image] HELP
[Image][Image][Image][Image][Image][Image][Image]  [Image][Image] [Image][Image
][Image][Image][Image][Image]
[Image][Image][Image][Image][Image][Image][Image]  [Image][Image] [Image][Image
][Image][Image][Image]       [Image][Image]
     
                             [Image][Image]
       [Image]  Home [Image]Hotmail[Image]Directory[Image]Shopping[Image] Peopl
e[Image]              [Image][Image][Image]
                                                                          & Cha
t
[Image][Image][Image][Image][Image][Image][Image]  [Image][Image] [Image][Image
][Image][Image][Image][Image][Image][Image]
   Return every     Win Premiership
   day!             Tickets
   Make MSN your    Arsenal v
   homepage         Liverpool                 February 08


  [Image]
       News                JOIN IN THE FUN!        [Image]      [Image]
       Computing              [Image][Image] BUILD [Image]       MESSAGE
       Business                      YOUR own free               CENTRE


                                                              [Image]
       Personal                      web site,     [Image]      username:
       Finance                       create an
       Recruitment                   online photo               [Image]
       Entertainment                 album, and                 password:
       Games                         send instant
       Football                      messages --                Sign up
                                     all from one               for free
       Live Events                   place.
       Classifieds                                              e-mail
       Cars                          [Image]
       Property                                                 [Image]
                                     Valentine's
       Travel                        shopping
       Learning                      [Image] Find a
       Communicate
       [Image]                       new home
                                     [Image] Hijack
                                     drama latest
                                     [Image] Win

                                     the top 10 of
                                     everything!
                           [Image]
                           [Image]     [Image]
                           [Image]
                           Totaljobs.com shareware

                           [Image] Check [Image]
                           your          Yellow
                           horoscope     Pages
                                         search

                           [Image] Free  [Image]
                           Internet      Buy books
                           access        from
                                         Amazon

                           [Image]


[Image]
  CHANGE Content / Layout / Colour                                   HELP

[Image]Only          Is my domain available?
on MSN
[Image] Play                  Check whether the domain you want is still availa
ble.
                              You can check for .com/ .net/ .org/ etc.
against the          [Image]

==================search.pl
#!/usr/local/bin/perl

use CGI;

$query=new CGI;
$text_file="/tmp/homepage.txt";

print "Content-type: text/html\n\n";
if ($query->param){ ##  The form has been submitted by user
  $text_message=$query->param('text_message');  ##  Read the CGI data

  if ($text_message ==1 || $text_message=~ /^\s+$/){
    print "<B>Search string cannot be empty. Try again</B><br>\n";
  }else{
    # search not case-sensitive
    $text_message = uc($text_message);
    open(TXT,$text_file) || die $!;

    $flag_found=0;
    while(<TXT>){
      if (/$text_message/i){  ## Case insensitive match
        $flag_found++;
        last;
      }
    }
    close(TXT);

   if ($flag_found){
      ##Write code when matches are found
    }else{  ##  No match found
      print "<B>Search string not found. Try again</B><br>\n";
    }
  }
}

print "<HTML> <HEAD>";
print "<FORM NAME=rst ACTION=\"".$ENV{SCRIPT_NAME}."\"METHOD=POST>\n";
print "Enter search string: <INPUT TYPE=TEXT NAME=text_message VALUE=\"\">\n";
print "<P><INPUT TYPE=SUBMIT VALUE=\"Search Now!!\">\n";
print "</FORM>\n";
0
 
ggdAuthor Commented:
I played around with the code you suggested above and it worked.
Thanks. :)
0
All Courses

From novice to tech pro — start learning today.