I've been racking my brain about this code for days. It takes the user's input (a name or part of a name), checks to see if it has only one word, then it should use a grep function to find the person's answer in the array that contains the file. But it doesn't even print out anything. When I stopped using grep and used a "foreach" loop to put it inot a variable, then using an "if" loop to first check the line for the word, and pushing it into array, it just displays the whole file.
#!/usr/bin/perl #Start the modules use strict; use CGI; #Create a new CGI object my $Page = new CGI; #Take the inoformation from the CGI module my $Info = $Page->param("words"); #Print out an HTML MIME Header print $Page->header; #Print out the proper HTML print <<HTMLSTUFF; <html> <head> <title> Your results </title> <style language="text/css"> <!-- p { font-family: Arial, Times New Roman, Garamond; } //--> </style> </head> <body bgcolor="lightGreen"> HTMLSTUFF #Check to see if the information has only one word if ($Info =~ m/ /) { print "<p>Please use only one word.</p>\n"; print <<MOREHTML; </body> </html> MOREHTML exit; } #Make sure the file can be opened unless (open (FILE, "<../Names.html")) { print "<p>Make sure the file is there</p>\n"; print <<MOREHTML; </body> </html> MOREHTML exit; } #Read the whole thing into an array my @Lines = <FILE>; #Close the file close (FILE); #Use "grep" to find the goods my @Names =~ grep("$Info", @Lines); my $i; ############The whole thing #Print out the word "Results print "<p>Here's the results</p>\n"; #Print out all of that shit #Do a "for" loop to print each of the names for ($i=0; $i<=@Names; $i++) { print "<p>$Names[$i]</p>\n"; } ##########End it all print "</body>\n"; print "</html>\n";
Please Perl Monks, you are my only hope...

In reply to Looking through a file... by Anonymous Monk

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.