Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Question 1: guessing the answer in under 10 tries. The statement you missed is "return". It causes the subroutine to return to whoever called it immediately. All loops that it may be in at that point are automatically exited directly to the caller, bypassing any and all other statements in the subroutine.

Question 2: chomping. Your $guess is a number. Perl's DWIMery trick with numbers is to treat anything that looks like a number as a number. So if you have a string such as "12afsd4583", perl will treat it as the number 12. e.g.,

$ perl -e 'print 0+"12afsd4583";print $/' 12
In your case, the string $guess really is "123\n". Perl sees the "123" part as a number, and then it sees stuff that isn't numeric and cuts it off at that point. So it will do an implicit 'chomp'-like action.

However, with $again, you're comparing it with the eq operator. Perl can't know, based on string context, whether the \n is important or not, so you have to be explicit. Another way to avoid the chomp is to use a less-precise match, such as a regular expression, e.g., $again =~ /y/i will match anything with a 'y' or a 'Y' in the string, whether it's followed by a carriage return or not.

BTW - I pointed out the strict comment because while I was rewriting your code (or, rather, mostly just reformatting), I came upon a problem that would have been caught sooner had strict been used. Not a problem in your code, but in an intermediate code between what you had and what I ended up with. ;-) Another advantage of using strict when it's not necessarily needed: adding features and refactoring become easier. Or at least safer. ;-)


In reply to Re^3: Number Guess by Tanktalus
in thread Number Guess by cryptoquip

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-19 15:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found