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