Perl Monks I'm new to you, Perl and programming in general. As I build my skills, I am fully aware that my questions, as well as my knowledge of Perl, are at best basic, at least trivial, so please forgive on this point, on all I others I will be very careful to comply with posting regulations, which I have read thoroughly.

I am writing a basic trivia game, and for some reason, this if statement will not work:

if ($guess eq $correct[$i]) { # if guess is correct, reply print "You Rock!\n"; $i++; } else { print "You Suck!\n"; # if incorrect, reply and save answer to + be calculated @incorrect = (@incorrect, $guess);
I have looked up several examples in my lama book, all of them seemingly in agreement with mine, as far as formatting and syntax go. This is inside of a loop, in more code, but I have spent several days breaking it into pieces, and have pinpointed this piece as the problem.

Regardless of the answer given, it skips the first "if" and goes right to the "else", i.e., if I give the correct answer, it recognizes it as incorrect, saves it accordingly, and prints the "else" reply every time. I have checked and double-checked, in several test routines, that all of my variables hold the correct values, so I know the problem is not there. However, just in case I missed something there:

my @questions = ("Name the definitive rock band.." , "What is their be +st song?" , "What is their best album?"); my @correct = ("The Rolling Stones" , "Moonlight Mile" , "Sticky Finge +rs"); my $grade = 100 - length(my @incorrect) * 33.3; my @incorrect; my $i = 0; my $guess;

After reading one reference, I saw some similar code in example with this added between the "if and "else":

} elsif ($i < 2) { $i = $i + 1;
However this only compounded the problem. I do believe that this piece is an insight to what I'm doing wrong, or missing, but I can't seem to wrap my head around it. I have looked up control structures, syntax, etc. of this piece in several places, lama book (first edition), perlop links from here, their FAQ and the camel book, which I really am not even equipped to navigate just yet.

Please note, that the simplicity, i.e. lack of regular expressions, not using a hash for the questions and answers and the like, I fully realize, I began this in my first month and have now been asked to de-bug the original code, with out upgrading it to what I've learned since, in order to completely master my use of control structures, before I move on to my next project. Also, I certainly am not asking for ya'll to do my work for me, I am very aware of the problems with that, for you and my skills, but I have been trying to figure my mistake out for quite some time now.

It may be something easily referenced, if it is, I didn't know where to look.

Basically, I really would very much like a hint as to what I'm doing wrong in my control structure, this is a very basic skill, and though I am moving forward quickly, if I can't master this small thing, I certainly won't get very far :)

Thank you for your time, Perl Monks

In reply to Control Structure problem, mistake can't be found by koolgirl

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.