and / or in searching a text file

I have the following code that searchs a text file (checks for empty/no matchs) returns the result in a web page (HTML) format.

Can someone please tell me how to incorporate the options to use "AND" (and "OR") while doing a search.

So the script should be able to look for exact match if the user chooses "AND". If "OR" chosen, any matches fitting the criteria should be displayed in a web page.

Thanks.

Script
------------

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

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

# let's not make the search case-sensitive, ok?
$ucsearch = uc($search);


&parseInput;

if (length($search) <=0 || $search =~/^\s+$/)
{
&empty;
}
else
{
open(THE_FILE, "email.txt");

$flag_found=0;

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

#-------------------------------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>  
</BODY>
</HTML>
EOF

}
ggdAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ggdAuthor Commented:
Adjusted points to 50
0
amitpagarwalCommented:

try setting up a flag say $ORAND which has 1 for OR and 0 for AND.

When the value of flag is 0 (ie, AND),
whenever a word is matched delete it from the search arguement array. Thus after the whole search, if this array is empty, u found all the words.

In the case of 1 (ie, OR), come out of the loop as soon as a single word is matched.

thanks,
amit
0
ggdAuthor Commented:
I am not really sure how to do that. I am new at PERL scripting.I might need a little help in writing the code.
Or editing the one I have posted above.
Please.
Thanks.
0
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

ggdAuthor Commented:
I have re-edited to code to include the option of using 'and' & 'or' (using a flag).

However, now the problem is when it finds more than one option it goes to the output function and creates multiple web pages within a page

How do I only output every match into a static template instead ? So for instance if I have more than one match, it will only print out the multiple lines one after the other into a static template.

Any help would be appreciated.
Thanks.

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

&parseInput;

read(STDIN, $save_string, $ENV{CONTENT_LENGTH});

@prompts = split(/&/,$save_string);
  foreach (@prompts)
    {
     ($tmp1, $tmp2) = split(/=/,$_);
      $tmp2 =~ s/\x2b/\x20/g;
      $tmp2 =~ s/%2C/\x2c/g;
      $tmp2 =~ s/%28/\x28/g;
      $tmp2 =~ s/%29/\x29/g;
      $fields{$tmp1}=$tmp2;
  }

$keys = $fields{'search'};
$search_type = $fields{'search_type'};

if (length($keys) <=0 || $keys =~/^\s+$/)
{
&empty;
}
else
{
      @search_key = split (/\x20/,$keys);
      $flag_found=0;
open(THE_FILE, "phone_book.dat");
while (<THE_FILE>)
{
$in_line = $_;

      if ($search_type eq "and")
   { $found_flag = "Y"; }
  else
   { $found_flag = "N"; }

  foreach (@search_key) #check for each search key in the file line
  {
if ( $in_line =~ m/$_/i)       # Is key in the data line ?
      {
      if ($search_type eq "or")   # Yes-
       {
        $found_flag = "Y";             # If its a OR search all we
        last;                          # need id one match
       }
   }
  else                           #No-
   {
     if ($search_type eq "and")       # If its a AND search all we
      {                               # need is one non-match
       $found_flag = "N";
       last;
      }
   }

  } #end of ForEach Loop


 if ( $found_flag eq "Y" )
{
    $flag_found++;
    $in_line = $_;
($email, $name, $phone_num, $dep ) = split(/:/,$in_line);
&output;
}
} #End of read loop

if (!$flag_found)
{
&nomatch;
}

}
      

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

sub  output
{
print <<"EOF";

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

</HEAD>

<BODY BGCOLOR="#FFFFFF">

<table width=430 border=0>
<tr><td>Name</td><td></td><td>$email<br></td></tr>
<tr><td>Email</td><td></td><td>$name<br></td></tr>  
<tr><td>Ext.</td><td></td><td>$phone_num<br></td></tr>
<tr><td>Division</td><td></td><td>$dep<br></td></tr>
</table>
<p></p>

<FORM METHOD=GET action="searchgwen5.pl">

Enter the name of the employee or email address you would like to search for:
<P>
<INPUT NAME=search SIZE=25> <INPUT TYPE=SUBMIT VALUE=Search>

</FORM>

</BODY>
</HTML>
EOF

}

====================
The source of the result looks like this :
========
<html>
<head>
<title>Results</title>
</head>
<body>
<table width=430 border=0>
<tr align='left' valign='top'>
<td><font face='Verdana, Arial, Helvetica, sans-serif' size='-1'><b>Name</b></font></td>
<TD><font face='Verdana, Arial, Helvetica, sans-serif' size='-1'>Adel Al-Awadhi</font></TD>
</tr>
<tr align='left' valign='top'>
<td><font face='Verdana, Arial, Helvetica, sans-serif' size='-1'><b>Email</b></td></font>
<TD><font face='Verdana, Arial, Helvetica, sans-serif' size='-1'>aalawadhi@wataniya.com
</font></TD>
</tr>
</table>
</body>
</html>
<html>
<head>
<title>Results</title>
</head>
<body>
<table width=430 border=0>
<tr align='left' valign='top'>
<td><font face='Verdana, Arial, Helvetica, sans-serif' size='-1'><b>Name</b></font></td>
<TD><font face='Verdana, Arial, Helvetica, sans-serif' size='-1'>Adel Esfendeyar</font></TD>
</tr>
<tr align='left' valign='top'>
<td><font face='Verdana, Arial, Helvetica, sans-serif' size='-1'><b>Email</b></td></font>
<TD><font face='Verdana, Arial, Helvetica, sans-serif' size='-1'>aesfendeyar@wataniya.com
</font></TD>
</tr>
</table>
</body>
</html>
0
guadalupeCommented:
Do you want seperate tables or all results in one table...?
0
ggdAuthor Commented:
Hi quadalupe,

Thanks for answering. I 'd like to have all the answers in one table if it's possible, please.
Thanks.
G.D
0
guadalupeCommented:
Try this:


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

&parseInput;

#CHECK THIS BECAUSE ITS WHAT WERE DOING IN &parseInput - See if one of the two is not repetitious
read(STDIN, $save_string, $ENV{CONTENT_LENGTH});

&outputHTML_head;


@prompts = split(/&/,$save_string);
  foreach (@prompts)
    {
     ($tmp1, $tmp2) = split(/=/,$_);
      $tmp2 =~ s/\x2b/\x20/g;
      $tmp2 =~ s/%2C/\x2c/g;
      $tmp2 =~ s/%28/\x28/g;
      $tmp2 =~ s/%29/\x29/g;
      $fields{$tmp1}=$tmp2;
  }

$keys = $fields{'search'};
$search_type = $fields{'search_type'};

if (length($keys) <=0 || $keys =~/^\s+$/)
{
&empty;
}
else
{
@search_key = split (/\x20/,$keys);
$flag_found=0;
open(THE_FILE, "phone_book.dat");
while (<THE_FILE>)
{
$in_line = $_;

if ($search_type eq "and")
   { $found_flag = "Y"; }
  else
   { $found_flag = "N"; }

  foreach (@search_key) #check for each search key in the file line
  {
if ( $in_line =~ m/$_/i)       # Is key in the data line ?
{
      if ($search_type eq "or")   # Yes-
       {
        $found_flag = "Y";             # If its a OR search all we
        last;                          # need id one match
       }
   }
  else                           #No-
   {
     if ($search_type eq "and")       # If its a AND search all we
      {                               # need is one non-match
       $found_flag = "N";
       last;
      }
   }

  } #end of ForEach Loop


 if ( $found_flag eq "Y" )
{
    $flag_found++;
    $in_line = $_;
($email, $name, $phone_num, $dep ) = split(/:/,$in_line);
&output;
}
} #End of read loop

if (!$flag_found)
{
&nomatch;
}

}

&outputHTML_footer;


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


sub  outputHTML_head()
{
print <<"EOF";

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

</HEAD>

<BODY BGCOLOR="#FFFFFF">

<table width=430 border=0>
EOF
}

sub  output
{
print <<"EOF";

<tr><td>Name</td><td></td><td>$email<br></td></tr>
<tr><td>Email</td><td></td><td>$name<br></td></tr>  
<tr><td>Ext.</td><td></td><td>$phone_num<br></td></tr>
<tr><td>Division</td><td></td><td>$dep<br></td></tr>

}


sub outputHTML_footer()
{

print <<"EOF";

</table>
<p></p>

<FORM METHOD=GET action="searchgwen5.pl">

Enter the name of the employee or email address you would like to search for:
<P>
<INPUT NAME=search SIZE=25> <INPUT TYPE=SUBMIT VALUE=Search>

</FORM>

</BODY>
</HTML>
EOF

}




0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ggdAuthor Commented:
Hi guadalupe,

I edited it :
I still get errors. It only go through the empty query sub, and returns nothing else.
Any ideas ?
Thanks.
--------
Script:-
#!/usr/bin/perl
print "Content-Type: text/html\n\n";

&parseInput;
#&outputHTML_head;

read(STDIN, $save_string, $ENV{CONTENT_LENGTH});

#&outputHTML_head;

@prompts = split(/&/,$save_string);
  foreach (@prompts)
    {
     ($tmp1, $tmp2) = split(/=/,$_);
      $tmp2 =~ s/\x2b/\x20/g;
      $tmp2 =~ s/%2C/\x2c/g;
      $tmp2 =~ s/%28/\x28/g;
      $tmp2 =~ s/%29/\x29/g;
      $fields{$tmp1}=$tmp2;
  }

$keys = $fields{'search'};
$search_type = $fields{'search_type'};

if (length($keys) <=0 || $keys =~/^\s+$/)
{
&empty;
}
else
{
      @search_key = split (/\x20/,$keys);
      $flag_found=0;
open(THE_FILE, "emailfinal2.txt");
while (<THE_FILE>)
{
$in_line = $_;

      if ($search_type eq "and")
   { $found_flag = "Y"; }
  else
   { $found_flag = "N"; }

  foreach (@search_key) #check for each search key in the file line
  {
if ( $in_line =~ m/$_/i)       # Is key in the data line ?
      {
      if ($search_type eq "or")   # Yes-
       {
        $found_flag = "Y";             # If its a OR search all we
        last;                          # need id one match
       }
   }
  else                           #No-
   {
     if ($search_type eq "and")       # If its a AND search all we
      {                               # need is one non-match
       $found_flag = "N";
       last;
      }
   }

  } #end of ForEach Loop


 if ( $found_flag eq "Y" )
{
    $flag_found++;
      $in_line = $_;
($email, $name, $phone_num, $dep ) = split(/:/,$in_line);
&output;
}
} #End of read loop

if (!$flag_found)
{
&nomatch;
}

}
      
&outputHTML_footer;

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

sub parseInput()
{
print <<"EOF";

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

</HEAD>

<BODY BGCOLOR="#FFFFFF">

<table width=430 border=0>
EOF
}

sub output
{
print <<"EOF";

<tr><td>Name</td><td></td><td>$email<br></td></tr>
<tr><td>Email</td><td></td><td>$name<br></td></tr>  
<tr><td>Ext.</td><td></td><td>$phone_num<br></td></tr>
<tr><td>Division</td><td></td><td>$dep<br></td></tr>
EOF
}


sub outputHTML_footer()
{

print <<"EOF";

</table>
<p></p>

<FORM METHOD=GET action="searchgwen8.pl">

Enter the name of the employee or email address you would like to search for:
<P>
<INPUT NAME=search SIZE=25> <INPUT TYPE=SUBMIT VALUE=Search>

</FORM>

</BODY>
</HTML>
EOF

}

#-My output looks like this -#
EOF } sub output() { print <<"EOF";  Name  
 
Email  
 
Ext.  
 
Division  
 
You entered an empty query  

Enter the name of the employee or email address you would like to search for:
 
-------
SOURCE OF OUTPUT (HTML)
<HTML>
<HEAD>
<TITLE> New Document </TITLE>

</HEAD>

<BODY BGCOLOR="#FFFFFF">

<table width=430 border=0>
EOF
}

sub output()
{
print <<"EOF";

<tr><td>Name</td><td></td><td><br></td></tr>
<tr><td>Email</td><td></td><td><br></td></tr>  
<tr><td>Ext.</td><td></td><td><br></td></tr>
<tr><td>Division</td><td></td><td><br></td></tr>

You entered an empty query

</table>
<p></p>

<FORM METHOD=GET action="searchgwen8.pl">

Enter the name of the employee or email address you would like to search for:
<P>
<INPUT NAME=search SIZE=25> <INPUT TYPE=SUBMIT VALUE=Search>

</FORM>

</BODY>
</HTML>
0
ggdAuthor Commented:
guadalupe,

I played around with the order of the functions and it works.Thanks.
How do I award u the points to this question.

-
G.D
0
guadalupeCommented:
First regect the answer pending...I will get the mail that you rejected and then I will post an "answer".  Thanks!!
0
ggdAuthor Commented:
Re-wrote the script.
0
guadalupeCommented:
Thanks again!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.