Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

RE: RE: Re: Minor query

by turnstep (Parson)
on Jun 25, 2000 at 00:01 UTC ( [id://19734]=note: print w/replies, xml ) Need Help??


in reply to RE: Re: Minor query
in thread Minor querry!!

All these loops are so long! Labels and status variables and chomping, oh my!

How about this:

{ print "Number?\n"; redo unless <STDIN> =~ /^\d+$/; }

Or, to save the value (which can be useful :)

{ print "Number?\n"; redo unless <STDIN> =~ /^(\d+)$/ and $number=$1; }

But my personal favorite would be simply:

print "Number?\n" while <STDIN> !~ /^\d+$/;

Replies are listed 'Best First'.
RE: RE: RE: Re: Minor query
by merlyn (Sage) on Jun 25, 2000 at 16:58 UTC
    And turnstep said:
    But my personal favorite would be simply:
    print "Number?\n" while <STDIN> !~ /^\d+$/;
    Of course, you'll not have the number you read, unless you look at $& (expensive) or put \d+ inside parens, and then look at $1.

    -- Randal L. Schwartz, Perl hacker

      Yes, I meant to add a:

      $number = $&;
      which would be expensive, but considering that the script must stop for user input with the STDIN, probably not a big deal in this case. Parens would be smoother, but make the initial statement larger. IMO, of course. :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-04-19 08:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found