in reply to Modifying loop structure and placement

(#print "Enter your guess: \n\n > "; ">" in book's script; why?)

Just to provide a prompt for entering a number, ">" can be "%" or any other character for that matter, like you don't want the user to think that the scrip is hanging or something...

Now to return to line 4 like you want just use a label, a label is used in conjunction with goto, so the first segment of your code can be re-written as:

until ($isover) { clear_the_screen(); print "Find the secret number between 1 and 100: \n\n"; LABEL: print "Enter your guess: \n\n % "; #">" #in book's scrip +t; why? chomp($userguess = <STDIN>); #following "while" loop added by me in a "challenge" while ($userguess !~ /^[+-]?\d+$/ ) { #testing to see whether user + entry is a number print "\nYour guess must be a number. Try again. \n\n"; goto LABEL; }
Notice, I did not use "chomp($userguess = <STDIN>);" in line 11 since the flow would go from 4 four onwards once again...

I'd stress on using the pragmas:

use strict; use warnings;
these can be very helpful at ensuring that you don't mistype a variable name and can give you helpful warnings and thus greatly reduce the debugging time..

with $totalguesses, you can't return outside a subroutine, eval statement or do{}, hence using a print statement instead...

("if" loop beginning on line 17, pressing <Enter> after the 3 "elsif" choices returns the code to line 4)

Not entirely correct, what returns the code to line 4 is that the $isover in until(){} is not defined yet and hence this observation...

There's an extensive repetitive writing of certain statements in your code, that warrants taking a look at Perl subroutines which can enhance the logic-flow interpretation of the code..

Update: I did not mention that I pre-assigned $secretnumber with the value of 100....
Here's your code once again with places for you to notice being commented accordingly:
use strict; use warnings; my $isover; #using strict requires variables declaration wit +h 'my'. my $totalguesses; my $reply; until ($isover) { clear_the_screen(); print "Find the secret number between 1 and 100: \n\n"; LABELED: print "Enter your guess: \n\n % "; #labeling a posi +tion my $userguess; chomp($userguess = <STDIN>); while ($userguess !~ /^[+-]?\d+$/ ) { print "\nYour guess must be a number. Try again. \n\n"; goto LABELED; } $totalguesses++; my $secretnumber=100; if ($userguess == $secretnumber) { clear_the_screen(); print "You guessed it! Press <Enter> to return to the Main Me +nu.\n\n"; chomp($reply = <STDIN>); $isover = 1; #Defines $isover hence the evaluation would retu +rn true #the until(){} block would exit then... }elsif ($userguess > 100) { print "\nYou chose a number higher than 100.<Enter> to try aga +in.\n"; chomp($reply = <STDIN>); }elsif ($userguess < $secretnumber) { clear_the_screen(); print "$userguess is too low. Press <Enter> to guess again.\n\ +n"; chomp($reply = <STDIN>); }elsif ($userguess > $secretnumber) { clear_the_screen(); print "$userguess is too high. Press <Enter> to guess again.\n +\n"; chomp($reply = <STDIN>); } } print "your guesses $totalguesses\n"; sub clear_the_screen{ }
Finally, checking the Perl Documentation at the website or through your console is so rewarding so make it your new-year's new habit :).... Best of luck :)

Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.

Replies are listed 'Best First'.
Re^2: Modifying loop structure and placement
by mikeraz (Friar) on Dec 24, 2009 at 18:19 UTC

    Um, do you really want to use <= and >=?

    The tests are for less and greater than.


    Be Appropriate && Follow Your Curiosity
      <= and >= are used in the script I copied from Ford's book. And now that you point this out, using that code doesn't make sense. That's just basic math structure, and even this Perl neophyte knows that. I will have to pay closer attention to the script I copy from his book. Thanks.
Re^2: Modifying loop structure and placement
by irvson (Sexton) on Dec 30, 2009 at 22:47 UTC

    Thanks for taking the time to post this extensive response. More for me to study. Note that the code came from JL Ford's book. I didn't create it, I copied it.

      Another "oops." The above reply is to the post above yours.
Re^2: Modifying loop structure and placement
by irvson (Sexton) on Dec 30, 2009 at 22:52 UTC
    First reply to the post below yours was meant as a reply to your post. (I didn't aim too well.)