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


In reply to Re: Verifying number input by dvergin
in thread Verifying number input by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.