Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Welcome to the Monastery
 
PerlMonks  

Re: Verifying number input

by dvergin (Monsignor)
on Dec 08, 2001 at 09:00 UTC ( #130383=note: print w/ replies, xml ) Need Help??


in reply to Verifying number input

A few comments. First, you appear to be using "use strict;". That's a good sign. It will save you a lot of grief in the long run.

You are using some indenting. But it is deceptive -- the indenting doesn't correspond to what you are doing. And several aspects of your logic would be much clearer with the help of some indenting that made the flow of your code clear.

Finally, and in answer to your question, here is a slightly re-worked version of your code that does what you want. Note that I also put in a feedback line for the 0-99.99 test that you do.

my $length = 0; while (1) { print "Enter the length: "; chomp ($length = <>); # checking for non-numeric characters unless ($length =~ /^[\d.]+$/){ print "\nPlease enter a number\n"; next; } last if 0 <= $length and $length <= 99.99; print "It must be between 0 and 99.99\n"; }
Your regex tested to see if there was anything that was not a digit. This approach /^[\d.]+$/ tests to see if between the beginning and the end of the string there are one or more characters that can only be digits or a period. If any other character appears in the string, the test fails.

There are other ways to attack this, but I have stayed fairly close to your logic. Hope this helps.

David

Update: coolmichael is right, of course. A revised regex might be: /^\d+(\.\d+)?$/

------------------------------------------------------------
"Perl is a mess and that's good because the
problem space is also a mess.
" - Larry Wall


Comment on Re: Verifying number input
Download Code
Re: Re: Verifying number input
by coolmichael (Deacon) on Dec 08, 2001 at 12:08 UTC
    For the most part, that will work but it will also match strings like 99.23.8427. I'm not sure if it will be a problem for the poster though, I just thought it important to point out.

    Michael.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (15)
As of 2014-04-17 18:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (453 votes), past polls