i am doing a search on txt files. so the files that include the search word have to be returned. but what i want to print is the date of the submission and the title of the submission, 2 fields within each file. the link has to be to the file. here is the code:
#!/usr/bin/perl # Define Variables # $basedir = '/htdocs/'; $baseurl = 'http://website.com'; @files = ('*.dtl'); $title = "Search Again"; $title_url = 'http://website.com/'; $search_url = 'http://website.com/'; # Parse Form Search Information &parse_form; # Get Files To Search Through &get_files; # Search the files &search; # Print Results of Search &return_html; 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 = `ls $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 is what we will search $string = join(' ', @LINES); $string =~ s/\n//g; #$string2 is the title of the document @string2 = ($LINES[0]); $string2 =~ s/\n//g; $string2 =~ s/\<h3\>//ig; $string2 =~ s/\<\/h3\>//ig; $string2 =~ s/title: //; #$string3 is the submission date of the document @$string3 = ($LINES[15]); $string3 =~ s/\n//g; $string3 =~ s/submitted: //; #chomp ($string3); $string3 = scalar localtime $string3; 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} = "$string2"; $titles2{$FILE} = "$string3; } } } sub return_html { print "Content-type: text/html\n\n"; print "<html>\n <head>\n <title>Results of Search</title>\n </head +>\n"; print "<body>\n <center>\n <h1>Results of Search in $title</h1>\n +</center>\n"; print "<b>Below are the results of your search sorted by submission + date:</b><p><hr size=7 width=75%><p>\n"; print "<ul>\n"; foreach $key (keys %include) { if ($include{$key} eq 'yes') { print "<li><a href=\"passtest.cgi?$key\">$titles{key}<br>$titles +2{$key}<br></a><br><br>\n"; } } print "</ul>\n"; print "<hr size=7 width=75%>\n"; print "Search Information:<p>\n"; print "<ul>\n"; print "<li><b>Terms:</b> "; $i = 0; foreach $term (@terms) { print "$term"; $i++; if (!($i == @terms)) { print ", "; } } print "\n"; print "<li><b>Boolean Used:</b> $FORM{'boolean'}\n"; print "<li><b>Case $FORM{'case'}</b>\n"; print "</ul><br><hr size=7 width=75%><P>\n"; print "<ul>\n<li><a href=\"$search_url\">Back to Search Page</a>\n" +; print "<li><a href=\"$title_url\">$title</a>\n"; print "</ul>\n"; print "<hr size=7 width=75%>\n"; print "</body>\n</html>\n"; }

In reply to malaga's hash/array/search problem by malaga

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.