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:
search help

 



alexz
stranger

Jul 30, 2001, 8:00 AM

Post #1 of 4 (1447 views)
search help Can't Post

i have a text file, and im trying to make a cgi script that will search the text file using a web-based search. this is the code i have so far, but it doesnt seem to work. Also, I'm assuming that when u write code in perl, u can save it as a cgi file within the perl builder program itself. is this the correct way to do it? thanks:

HTML File:

<html><head></head><body>
<form action="search.cgi" method="post">
Enter the words to search for:
<input type="text" name="name" size=30>
<input type="submit" value="search">
</form>
</body</html>

CGI Code:

#!/usr/bin/perl
$datafile = "data.txt";
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/~!/ ~!/g;
$FORM{$name} = $value;
}

$searchstr = $FORM{'name'};

open(INF,$datafile);
@mydata = <INF>;
close(INF);
print "Content-type:text/html\n\n";
print "<html><head><title>Search Results</title></head>\n";
print "<body><h3>Search Results</h3>\n";

@results = grep(/$searchstr/i,@mydata);

$search = $input{'search'};
if ($#results >= 0) {
foreach $variable (@results){
chomp($variable);
($num,$name,$status,$price)=split(/\|/, $variable);
if($name =~ m/$search/){
print "<b>$name</b> - $price
\n";
}
} else {
print "No results found.<p>\n";
}
print "</body></html>\n";




sigma
stranger

Jul 30, 2001, 10:58 AM

Post #2 of 4 (1434 views)
Re: search help [In reply to] Can't Post

You are missing one '}'

Code
if ($#results >= 0) { 
foreach $variable (@results) {
chomp($variable);
($num,$name,$status,$price)=split(/\|/, $variable);
if($name =~ m/$search/){
print "<b>$name</b> - $price\n";
}
}
} else {
print "No results found.<p>\n";
}

--
for ($!=0;$!<123;$!++) { print "$!\n"; }


alexz
stranger

Jul 30, 2001, 11:17 AM

Post #3 of 4 (1433 views)
Re: search help [In reply to] Can't Post

good call on the missing bracket, but it still doesnt seem to work.



alexz
stranger

Jul 30, 2001, 11:32 AM

Post #4 of 4 (1431 views)
Re: search help [In reply to] Can't Post

i've tried a lot of editing and a lot of different ways to execute the code, but everytime i search using the HTML interface, the cgi script in text format shows up. does anyone know a simpler way of writing this code? all i need is a web-based interface to search a specific text file for whatever keywords are entered by the user. thanks for any help.


 
 


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

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