(#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.

In reply to Re: Modifying loop structure and placement by biohisham
in thread Modifying loop structure and placement by irvson

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.