Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re^2: Guess Number

by ysth (Canon)
on Sep 01, 2006 at 21:42 UTC ( [id://570838]=note: print w/replies, xml ) Need Help??

in reply to Re: Guess Number
in thread Guess Number

You've moved "Guess the Code" into the loop, which may or may not be a good thing. I'd prefer do { } while to bare-BLOCK redo. There's no real need to strip newlines off of numeric input if you aren't storing it anywhere.

How about:

use strict; use warnings; print "Guess the Code:"; 1 while <> != (1+int rand 10); print "\n Time taken to break the Code ..: ", time()-$^T, " seconds\n +";
(use warnings is only included to show that "42\n" numifies to 42 with no warning; it really should be removed to prevent warnings on more seriously non-numeric input.)

Replies are listed 'Best First'.
Re^3: Guess Number
by GrandFather (Saint) on Sep 01, 2006 at 22:03 UTC

    I considered do {} while, but that required declaring the variables outside the loop and, besides, I felt there was some virtue in showing OP the bare block trick.

    Using the comma operator:

    use strict; use warnings; 1 while (print "Guess the Code: "), <> != (1+int rand 10); print "\n Time taken to break the Code ..: ", time()-$^T, " seconds\n +";

    gets the prompt printed for each iteration.

    Using $^T is nice btw. Something I'd not have thought of - I'm still learning. :)

    DWIM is Perl's answer to Gödel

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://570838]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-24 10:28 GMT
Find Nodes?
    Voting Booth?

    No recent polls found