Article View: comp.lang.perl.misc
Article #313Newbie in need of help!!!
From: kmyers
Date: Mon, 22 Mar 1999 00:00
Date: Mon, 22 Mar 1999 00:00
352 lines
12387 bytes
12387 bytes
I'm a total Perl newbie who really needs help solving a problem. I've
downloaded a search script from the Matt's script archive site and and
modified it to work on a Windows NT machine. The problem I've having is that
after it searches a directory and returns all matching files it prints the
words "File not found" twice. The search aspect works fine, I just don't
understand why the "File not found message is being printed also. If you can
tell me what the problem is please email me at kmyers@syneractive.com Thank
you.
Here's a copy of the code:
#!/usr/bin/perl
#######################################################################
#######
# Simple Search Version 1.0
#
# Copyright 1996 Matt Wright mattw@worldwidemart.com
#
# Created 12/16/95 Last Modified 12/16/95
#
# Scripts Archive at: http://www.worldwidemart.com/scripts/
#
#######################################################################
#######
# COPYRIGHT NOTICE
#
# Copyright 1996 Matthew M. Wright All Rights Reserved.
#
#
#
# Simple Search may be used and modified free of charge by anyone so
long as #
# this copyright notice and the comments above remain intact. By using
this #
# code you agree to indemnify Matthew M. Wright from any liability that
#
# might arise from it's use.
#
#
#
# Selling the code for this program without prior written consent is
#
# expressly forbidden. In other words, please ask first before you try
and #
# make money off of my program.
#
#
#
# Obtain permission before redistributing this software over the
Internet or #
# in any other medium. In all cases copyright and header must remain
intact.#
#######################################################################
#######
# Define Variables
#
#$basedir = '/mnt/web/guide/worldwidemart/scripts/';
#$baseurl = 'http://worldwidemart.com/scripts/';
#@files = ('*.shtml','demos/links/*.html','demos/guest/*.html');
#$title = "Matt's Script Archive";
#$title_url = 'http://worldwidemart.com/scripts/';
#$search_url =
'http://worldwidemart.com/scripts/demos/search/search.html';
$counter = 0;
$total_returned = 0;
$basedir = 'C:\inetpub\wwwroot\kosa';
$baseurl = 'http://landini/kosa/';
@files = ('*.htm','*.html','*.asp');
$title = "Kosa Web Site";
$title_url = 'search.html';
$search_url = 'http://landini/kosa/search.html';
@directories = ('C:/inetpub/wwwroot/kosa/poly/',
'c:/inetpub/wwwroot/kosa/resins/',
'c:/inetpub/wwwroot/kosa/staple/',
'C:/inetpub/wwwroot/kosa/techfila/',
'C:/inetpub/wwwroot/kosa/textfila/',
'C:/inetpub/wwwroot/kosa/tire/');
@url = ('http://landini/kosa/poly/',
'http://landini/kosa/resins/',
'http://landini/kosa/staple/',
'http://landini/kosa/techfila/',
'http://landini/kosa/textfila/',
'http://landini/kosa/tire/');
# Done
#
#######################################################################
#######
# Parse Form Search Information
&parse_form;
# Get Files To Search Through
&get_files;
# Search the files
&search;
# Print Results of Search
&print_header;
&print_results;
foreach $subscript (@directories)
{#begin foreach
$baseurl = $url[$counter];
$basedir = $subscript;
&get_files;
&search;
&print_results;
$counter = $counter + 1;
}#end foreach
&print_footer;
sub parse_form {
# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
# Split the name-value pairs
@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;
$FORM{$name} = $value;
}
}
sub get_files {
chdir($basedir);
foreach $file (@files) {
$ls = `dir $file`;
@ls = split(/\s+/,$ls);
foreach $temp_file (@ls) {
if (-d $file) {
$filename = "$file$temp_file";
if (-T $filename) {
push(@FILES,$filename);
}
}
elsif (-T $temp_file) {
push(@FILES,$temp_file);
}
}
}
}
sub search {
@terms = split(/\s+/, $FORM{'terms'});
foreach $FILE (@FILES) {
open(FILE,"$FILE");
@LINES = <FILE>;
close(FILE);
$string = join(' ',@LINES);
$string =~ s/\n//g;
if ($FORM{'boolean'} eq 'AND') {
foreach $term (@terms) {
if ($FORM{'case'} eq 'Insensitive') {
if (!($string =~ /$term/i)) {
$include{$FILE} = 'no';
last;
}
else {
$include{$FILE} = 'yes';
}
}
elsif ($FORM{'case'} eq 'Sensitive') {
if (!($string =~ /$term/)) {
$include{$FILE} = 'no';
last;
}
else {
$include{$FILE} = 'yes';
}
}
}
}
elsif ($FORM{'boolean'} eq 'OR') {
foreach $term (@terms) {
if ($FORM{'case'} eq 'Insensitive') {
if ($string =~ /$term/i) {
$include{$FILE} = 'yes';
last;
}
else {
$include{$FILE} = 'no';
}
}
elsif ($FORM{'case'} eq 'Sensitive') {
if ($string =~ /$term/) {
$include{$FILE} = 'yes';
last;
}
else {
$include{$FILE} = 'no';
}
}
}
}
if ($string =~ /<title>(.*)<\/title>/i) {
$titles{$FILE} = "$1";
}
else {
$titles{$FILE} = "$FILE";
}
}
}
sub print_header {
print "Content-type: text/html\n\n";
print "<HTML><HEAD>";
print "<BASE HREF='http://landini/kosa/'>";
print "<TITLE>KoSa: Search Results </TITLE>";
print "<SCRIPT language='JavaScript'>";
print "<!--";
print "function OpenView(newin) {";
print "road=window.open(newin,";
print "'console',";
print
"'resizable=no,scrollbars=no,width=201,height=251,top=0,left=0')";
print "}";
print "// -->";
print "</SCRIPT>";
print "</HEAD>";
print "<BODY BGCOLOR='#FFFFFF' ALINK='#000066' VLINK='#006600'
onLoad=\"self.name = 'main'\" TEXT='#000033'>";
print "<LINK REL=STYLESHEET HREF='sheet.css' TYPE='text/css'>";
print "<CENTER>";
print "<TABLE BORDER='0' CELLSPACING='0' CELLPADDING='0' WIDTH='600'>";
print "<TR>";
print " <TD WIDTH='600' COLSPAN='7'><A HREF='index.htm'><IMG
SRC='images/logo.gif' WIDTH='183' HEIGHT='71' HSPACE='0' ALIGN='Left'
ALT='KoSa' BORDER='0'></A><IMG SRC='images/topline.gif' WIDTH='266'
HEIGHT='49' HSPACE='0'><A
HREF='javascript:OpenView('console.htm');'><NOSCRIPT><A
href='console.htm' target='console'></NOSCRIPT><IMG
SRC='images/jump.gif' WIDTH='121' HEIGHT='49' BORDER='0' ALT='Jump to a
Business'></A><IMG SRC='images/topline.gif' WIDTH='15' HEIGHT='49'
HSPACE='0'><IMG SRC='images/toprt.gif' WIDTH='15' HEIGHT='49'><A
HREF='who.htm'><IMG src='images/buttons/who.gif' width='68' HEIGHT='22'
BORDER='0' alt='Who is KoSa?'></A><IMG src='images/buttons/sp.gif'
width='14' HEIGHT='22'><A HREF='products.htm'><IMG
src='images/buttons/products.gif' width='43' HEIGHT='22' BORDER='0'
alt='Products'></A><IMG src='images/buttons/sp.gif' width='14'
HEIGHT='22'><A HREF='locate.htm'><IMG src='images/buttons/locate.gif'
width='47' HEIGHT='22' BORDER='0' alt='Locations'></A><IMG
src='images/buttons/sp.gif' width='14' HEIGHT='22'><A
HREF='whatsnew.htm'><IMG src='images/buttons/whatsnew.gif' width='63'
HEIGHT='22' BORDER='0' alt='What's New?'></A><IMG
src='images/buttons/sp.gif' width='14' HEIGHT='22'><A
HREF='contact.htm'><IMG src='images/buttons/contact.gif' width='37'
HEIGHT='22' BORDER='0' alt='Contact'></A><IMG
src='images/buttons/sp.gif' width='14' HEIGHT='22'><A
HREF='search.htm'><IMG src='images/buttons/search.gif' width='33'
HEIGHT='22' BORDER='0' alt='Search'></A><IMG
src='images/buttons/sp.gif' width='14' HEIGHT='22'><A
HREF='index.htm'><IMG SRC='images/buttons/home.gif' WIDTH='27'
HEIGHT='22' BORDER='0' ALT='home'></A><IMG src='images/buttons/sp.gif'
width='15' HEIGHT='22'></TD>";
print "</TR>";
print "<TR>";
print " <TD WIDTH='585' COLSPAN='5'><IMG SRC='images/headtop.gif'
WIDTH='585' HEIGHT='19'></TD>";
print " <TD WIDTH='1'><IMG SRC='images/sp.gif' WIDTH='1'></TD>";
print " <TD WIDTH='14'><IMG SRC='images/headtop.gif' WIDTH='14'
HEIGHT='19'></TD>";
print "</TR>";
print "<TR>";
print " <TD WIDTH='14' ROWSPAN='3' VALIGN='TOP'><IMG
SRC='images/side.jpg' WIDTH='14' HEIGHT='49'></TD>";
print " <TD WIDTH='1' BGCOLOR='#000099' ROWSPAN='2' VALIGN='TOP'><IMG
SRC='images/linetop.gif' WIDTH='1' HEIGHT='21'><BR><IMG
SRC='images/line.gif' WIDTH='1' HEIGHT='76'></TD>";
print " <TD WIDTH='15' ROWSPAN='2' VALIGN='TOP'><IMG
SRC='images/linesp.gif' WIDTH='15' HEIGHT='21'><IMG
SRC='images/headbot.gif' WIDTH='15' HEIGHT='28'></TD>";
print " <TD WIDTH='540' VALIGN='TOP'><IMG
SRC='images/headers/return.gif' WIDTH='122' HEIGHT='21' ALT='Search
Return'><IMG SRC='images/linesp.gif' WIDTH='418' HEIGHT='21'><IMG
SRC='images/headbot.gif' WIDTH='540' HEIGHT='28'></TD>";
print " <TD WIDTH='15' ROWSPAN='2' VALIGN='TOP'><IMG
SRC='images/side.jpg' WIDTH='15' HEIGHT='49'></TD>";
print " <TD WIDTH='1' BGCOLOR='#000099' ROWSPAN='2' VALIGN='TOP'><IMG
SRC='images/wsp.gif' WIDTH='1' HEIGHT='21'><BR><IMG
SRC='images/line.gif' WIDTH='1' HEIGHT='76'></TD>";
print " <TD WIDTH='14' ROWSPAN='3' VALIGN='TOP'><IMG
SRC='images/side.jpg' WIDTH='14' HEIGHT='49'></TD>";
print "</TR>";
print "<TR>";
print " <TD WIDTH='540' VALIGN='Top'><FONT CLASS=body SIZE=2
FACE='Arial, Helvetica, SanSerif'>";
print "Your search for the term <B>@terms</B> returned the following
documents: <P>";
print "<ul type='square'>\n";
}
sub print_results {
foreach $key (keys %include) {
if ($include{$key} eq 'yes') {
print "<li><a href=\"$baseurl$key\">$titles{$key}</a>\n";
$total_returned += 1;
}
}
}
sub print_footer {
print "</ul>\n";
print $total_returned;
print " </FONT><BR><IMG SRC='images/sp.gif' WIDTH='540'
HEIGHT='15'></TD>";
print " </TR>";
print " <TR>";
print " <TD BGCOLOR='#000099' WIDTH='572' COLSPAN='5'><IMG
SRC='images/sp.gif' WIDTH='572' HEIGHT='1'></TD></TR>";
print " <TR>";
print " <TD colspan='7'><TABLE><TR><TD><IMG src='images/sp.gif'
width='30' height='1'><FONT CLASS=copy SIZE=2 FACE='Arial, Helvetica,
SanSerif'>®1999 <A HREF='index.htm'>KoSa</A>. All rights
reserved.</FONT></TD><TD ALIGN='RIGHT'><FONT CLASS=copy SIZE=2
FACE='Arial, Helvetica, SanSerif'>Site created by <A
HREF='http://www.syneractive.com'
TARGET='new'>Syneractive</A>.</FONT><IMG src='images/sp.gif' width='30'
height='1'></TD></TR><TR><TD><IMG SRC='images/sp.gif' WIDTH='300'
HEIGHT='1'></TD><TD><IMG SRC='images/sp.gif' WIDTH='285'
HEIGHT='1'></TD></TR></TABLE></TD>";
print " </TR>";
print "</TABLE>";
print "</CENTER>";
print "</BODY>";
print "</HTML>";
}
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
Technical Details
Message-ID:
<7d600p$ups$1@nnrp1.dejanews.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 11f42b,885458bd6e0bd96,start X-Google-Attributes: gid11f42b,public X-Google-Thread: 10abbd,885458bd6e0bd96,start X-Google-Attributes: gid10abbd,public X-Google-Thread: 112333,885458bd6e0bd96,start X-Google-Attributes: gid112333,public X-Google-Thread: fa0ae,885458bd6e0bd96,start X-Google-Attributes: gidfa0ae,public From: kmyers@syneractive.com Subject: Newbie in need of help!!! Date: 1999/03/22 Message-ID: <7d600p$ups$1@nnrp1.dejanews.com> X-Deja-AN: 457614566 X-Http-Proxy: 1.0 x12.dejanews.com:80 (Squid/1.1.22) for client 12.6.52.254 Organization: Deja News - The Leader in Internet Discussion X-Article-Creation-Date: Mon Mar 22 17:52:30 1999 GMT Newsgroups: comp.lang.perl.misc,comp.lang.perl.module,fr.comp.lang.perl,atl.perl X-Http-User-Agent: Mozilla/4.05 [en] (WinNT; I)