Please note, this is a STATIC archive of website www.htmlgoodies.com from 16 Nov 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
Click Here!
Click Here!

Perl Primer Ten Assignment
A Possible Answer

     You have to set the count to zero outside the loops or the count will always be returned to zero after every loop. You then need to increase the count only when a line is written. That's a true count of the results. Finally, you need to add an extra line to print the results.

     My additions below are in bold.


$keyword=$FORM{keyword};
print "Content-type: text/html\n\n";
print "<h2>Here are the files we found</h2>\n\n";
$count = 0;
chdir("/this/must/be/an/absolute/path");

opendir(DIR, ".");
while($file = readdir(DIR))
{
    next if ($file !~ /.html/);
    open(FILE, $file);
    $foundone = 0;
    $title = "";
    while (<FILE>)
    {

       if (/$keyword/i)
       {
       $foundone = 1;
       }

       if(/<TITLE>/)
       {
       chop;
       $title = $_;
       $title =~ s/<TITLE>//g;
       $title =~ s/<\/TITLE>//g;
       }

    }
    if($title eq "")
    {
    $title = $file;
    }

    if($foundone)
       {
       print "<A HREF=/sub/sub/sub/$file>$title</A><BR>";
       $listed=1;
       $finalcount = $count++;
       }

close(FILE);

    }

closedir(DIR);

   print "<P>We found $finalcount pages.<P>";

    if($listed ne 1)
       {print "Sorry, nothing this time. <A HREF=/sub/sub/sub/searchengine.html>Try again</A>";}
    else
       {print "<P>That's all. Do you want a <A HREF=/sub/sub/sub/searchengine.html>new search?</A>";}


exit;





Close this window to return to the Primer