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:
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.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);
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":
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.} elsif ($i < 2) { $i = $i + 1;
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 MonksIn reply to Control Structure problem, mistake can't be found by koolgirl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |