Well jerrygarciuh,
Other people have explained your problem with getting parameters, but I will revisit it for a moment. You need to take the time to grok list and scalar context. It is obvious to me from your code that you dont quite get this idea and its very important that you do. BTW this isnt a disrespect, context in this regard is a perl peculiarity, and it usually takes a while to really understand it. But it is one of the main things that make perl as rich and expressive a language as it is, and accordingly absolutely must be learned.
Ok so on to the code. Ive read the posts that have been provided, suaveants is quite good, but both he and you overlooked a crucial issue, that is that if someone were to play your game over and over and over eventually your program would eat up all of the memory on your computer and crash. (Actually thats a lie, perl would first warn about deep recursion and then, I think :), terminate it before it affected overall system performance.)
Basically your code calls itself. For no reason. This is bad. Recursion should only be used when there is a predetermined point where the routine will no longer recurse. In your code there is no such condition. It is up to the user when to stop the recursion, which could mean never. To be honest at your level of programming, again no disrespect, but you shouldn't be doing recursion at all. Recursion is a tricky subject with many subtleties, until you have mastered a lot of other stuff, especially iteration first.
So here is suaveants edited version of your code, fixed to remove the recursion
#!/usr/local/bin/perl
use strict;
use warnings;
srand;
# moved subroutine calls to _after_ their declarations, not necessary,
+ but a good
# habit that can avoid nasty bugs in more complicated scenarios.
# this also means that input being an auxilliary sub to guess_routine
+needs to go
# before it
sub input {
my $arrayref=shift;
print "\nSo far you've guessed @$arrayref."
if $arrayref;
print "\nWhat is your guess?\n";
my $in=<STDIN>;
chomp ($in);
$in
}
sub guess_routine {
my $done=0;
until ($done) {
my @guesses;
print "I have a number from 0 to 19 inclusive.\nTry to guess i
+t.\n";
my $numb=int(rand(20));
my $in = input();
while ($in != $numb){
print($in > $numb ? 'Too high! ' : 'Too low! ');
# moved to trinary operator, just shrinks up
# code, if else isn't bad
@guesses = sort{$a <=> $b} (@guesses,$in);
# you were pushing onto the array, then sorting
# simply sorting array and value together shoule
# be more efficient
$in = input(\@guesses); # created a subroutine for
# this, since you had similar code in two places.
}
print "\nYes, $numb is the number!\n";
print "So you want another go?\n";
my $answer=<STDIN>; # no chomp, you check /^y/ chomp unecessar
+y
if ($answer !~/^y/i){
print "\nOK, bugger off then!\n";
$done=1;
}
}
}
guess_routine(); #moved the print and rand into the routine
# that way you don't have the code duplicated when user
# says y for new game...
Incidentally I've also fixed a couple of other minor points in the code. One would be that you are not producing a number 'between 1 and 19' but rather a number from 0 to 19 inclusive. Or 0<=$num<=19. The other would be that exit 0 is unnecessary in this situation (actually it was necessary because you were using recursion, which is an indication that you were doing something wrong.) Now when the user decides not to play another game the subroutine terminates and then so does the script.
Now a minor point jerrygarciuh but you _really_ aught to sit down and go through either perlman:perltoot or perlman:perlboot. While these are both ultimately aimed at object oriented perl, they are excellent perl tutorials in general. If you sit down and work through these two documents you will learn 10000 times more than you will by asking questions here. Dont get me wrong, I've watched your progress here and im not trying to discourage you, but working through the tutorials will lead you to answer many of your own questions which is a much better way to learn. Of course I and many of the other monks will be happy as always to help you out with any questions that these tutorial might raise.
Yves
--
You are not ready to use symrefs unless you already know why they are bad. -- tadmc (CLPM) |