Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Here is what I am trying to do. When the page loads, in an "if" loop, it checks the information of the $value variable against what is in the @Comm array. It goes through a "for" loop for each of the slices in the @Comment array. Every time when I check for bugs, it always find a problem with the "if" loop. Look over this and see if you can fix it.
#!/usr/bin/perl # Loads the CGI Module use CGI; # creates a new CGI object my $page = new CGI; # This will print a standard HTML header print $page->header; # Grab a named CGI parameter my $value = $page->param(Comic); ##########declare the names of the files for the loop @Comm = ('Images/Le_Comic.jpg', 'Images/Le_Comic2.jpg', 'Images/Le_Com +ic3.jpg', 'Images/Le_Comic4.jpg', 'Images/Le_Comic5.jpg', 'Images/Le_Comic6.jpg' +, 'Images/Le_Comic7.jpg', 'Images/Le_Comic8.jpg', 'Images/Le_Comic9.jpg' +); ########## #Open up the comments file open (FILE, '../Comment.txt') or die "It could not be found."; #Save the contents of the FILE handle to an array @Comment = <FILE>; #do a for loop to make it go through the test for ($i=0; $i<=#@Comment; $i++) { #Do an if loop to go through and save the right thing if ($value == $Comm[$i]) { $Como = $Comment[$i]; } } # Then: print "<HTML><HEAD>\n"; print <<ENDMETA; <META NAME="GENERATOR" CONTENT="Adobe PageMill 3.0 Win"> <TITLE>Animetion Station &gt; Webcomic</title> <LINK REL="stylesheet" HREF="../Style.css" TYPE="text/css"> ENDMETA print "</head><BODY>\n"; print <<ENDHTML; <P><CENTER><DIV STYLE="background-color: #0099FF; font-family: Comic S +ans MS; font-size: 20px; width: 200px; color: white;">Web Comics</div></center></p> <P><TABLE WIDTH="757" BORDER="0" CELLSPACING="1" CELLPADDING="0" HEIGH +T="294"> <TR> <TD WIDTH="14%" VALIGN="TOP" ALIGN="CENTER" HEIGHT="293"> <div class="Standard"> <P><CENTER><A HREF="../index.htm">[Home]</a> <HR> <A HREF="../Gallery.shtml">[Gallery]</a> <HR> <A HREF="../Manga.shtml">[Manga]</a> <HR> <A HREF="../Video_Review.shtml">[Video Reviews]</a> <HR> <A HREF="../Links.shtml">[Links]</a> <HR> <A HREF="../Web_Comic.shtml">[Web Comic]</a></center></td> <TD VALIGN="TOP" WIDTH="270" HEIGHT="131"> </div> </td> <TD WIDTH="86%" VALIGN="TOP" BGCOLOR="#3366ff"> <P><IMG SRC="../$value"></p><br> <p>$Como</p> <P><IMG SRC="../Images/Comic_Face.jpg" WIDTH="57" HEIGHT="54" ALIG +N="BOTTOM" BORDER="0" NATURALSIZEFLAG="3"> <IMG SRC="../Images/Comic_Face2.jpg" WIDTH="60" HEIGHT="55" ALIGN= +"BOTTOM" BORDER="0" NATURALSIZEFLAG="3"> <IMG SRC="../Images/Comic_Face3.jpg" WIDTH="60" HEIGHT="62" ALIGN= +"BOTTOM" BORDER="0" NATURALSIZEFLAG="3"> <IMG SRC="../Images/Comic_Face4.jpg" WIDTH="60" HEIGHT="60" ALIGN= +"BOTTOM" BORDER="0" NATURALSIZEFLAG="3"></td> </tr> </table> ENDHTML print "</body></html>\n";

Replies are listed 'Best First'.
Re: Printing the comment
by wog (Curate) on Oct 06, 2001 at 03:58 UTC
    Your problem is that you are commenting out the latter part of your for loop and thus the if statement is interpreted as in the second part of the for loop, and a syntax error. The offending code segment is:

    #do a for loop to make it go through the test for ($i=0; $i<=#@Comment; $i++) { #Do an if loop to go through and save the right thing if ($value == $Comm[$i]) { $Como = $Comment[$i]; } }

    When perl parses this, it interpretes it as:

    for($i=0; $i <= if ($value==$Comm[$i]) { ....

    Because the #@Comment ... starts a comment. You probably meant to use $#Comment instead. Alternately, it might be better to re-write that for loop to use the foreach-style:

    my $Como; foreach my $i (0..$#Comment) { $Como = $Comment[$i], last if $Comm[$i] == $value; } # ... and you should add error-checking: unless (defined $Como) { # do something here if we didn't find anything }

    update: but, you are trying to compare $value to string, so you should replace that == (which compares things as numbers) with an eq (which compares them as strings):

    # ... $Como = $Comment[$i], last if $Comm[$i] eq $value;

    (Note that you could write that statement using the if (...) { ... } form, too, of course. You may find that clearer.)

    In the future it would be a good idea to use strict and warnings. And taint checks.