Solved

Perl Double quote problem.

Posted on 2006-11-04
3
1,034 Views
Last Modified: 2013-12-25
Hello guys,im new to perl so i got a small problem here.Glad if anyone could help.
Im building a personal search engine and i would like to use double quotes to get the exact word/words.I got stuck and didnt know where to specify the double quotes to do the action.
 
My code:
#!c:/perl/bin/perl
read(STDIN, $buffer,$ENV{'CONTENT_LENGTH'});
@pairs = split(/&/,$buffer);
foreach $pair(@pairs){
($key,$value)=split(/=/,$pair);
$value =~ tr/+/ / ;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
#$value=~ s/\"//g; # strip double quotes
$formdata{$key}.="$value";
}
$search=$formdata{'search'};
open(INFO, "Docs.htm")or die "Can't open file for output"; # Open db for reading
@array=<INFO>;
close (INFO);
$count = 0;
print "Content-type:text/html\n\n"; #Content Header
print <<End_of_head;
<html>
<head><title>.::Search Results::.</title></head>
<body>
<h5>You Searched for :$search</h5>
End_of_head
   if($search eq "")
{
print "Error: Please type something";
}
else{
       #Both of the words
     $search =~ s/^\s+//;
     @cut = split(/\W/, $search);
     foreach $line  (@array){
     $count++;
     if (($line =~  /\b$cut[0]\b/i ) && ($line =~  /\b$cut[1]\b/i )) {
print <<End_of_line;
<font color="Red">Line<b> $count:</font> $line <br>
End_of_line
}
}
}
      #Exact word
    if ($search eq "'")
      {
       foreach $line  (@array){
     $count++;
     if ($line =~  /\b$search\b/i ){       
print <<End_of_line;
<font color="Red">Line<b> $count:</font> $line <br>
End_of_line      
      }
      }            
}
print <<End_of_Doc;
</body>
</html>
End_of_Doc
0
Comment
Question by:craziest
  • 2
3 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 400 total points
ID: 17873388
@cut = grep{defined}$search =~ /(\w+)|"([^"]+)"/g;
if( !@cut ){
   print "Error: Please type something";
}else{
     foreach $line  (@array){
         $count++;
         if ( !grep{$line !~  /\b\Q$_\E\b/i} @cut ){
print <<End_of_line;
<font color="Red">Line<b> $count:</font> $line <br>
End_of_line
          }
      }
}
0
 
LVL 3

Author Comment

by:craziest
ID: 17873423
Thats short and simple,worked well thanks!
if u dont mind,whats the regex for showing the tags in the file eg.
<TITLE> Hello</TITLE>
This doesnt show up when im searching.
Thanks again for your help.
0
 
LVL 84

Expert Comment

by:ozo
ID: 17873438
s/</&lt;/g, s/>/&gt;/g for $line;
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
disabled AD accounts info 3 69
Changing Audit Policies through scripting 5 56
Powershell / new-object PSObject rows 2 230
Control Number of Log Files -Perl 7 55
Introduction This tutorial will give you a fast look what you can do with WhizBase. I expect you already know how to work with HTML at least, and that you understand the basics of the internet and how the internet works. WhizBase is a server-s…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now