in reply to Verifying number input
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.
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.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"; }
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Verifying number input
by coolmichael (Deacon) on Dec 08, 2001 at 12:08 UTC |