It seems like you got it all down - so where is your question ?

I'll employ Psi::ESP and try to guess - you want to know how to check that a scalar contains a string that can be interpreted as a valid number - this is easy, as long as your definition of "number" is sane.

As you're talking about "price", I guess that your convention of "number" is "at least one digit from the range of 0 to 9, possibly followed by more such digits, possibly followed by a dot (to delimit the decimals), and then, if there was a dot, two more digits from the range 0 to 9".

After you've accepted this definition or have come up with your own definition, we need to translate this definition into a regular expression, since that's the easiest way of doing such things like conformance checks, as soon as you know regular expressions :

my $price = "100.09"; if ($price =~ / ^ # We begin at the start of the string [0-9]+ # At least one digit, possibly followed by more ( # Then (possibly, see below) follows \. # a dot [0-9][0-9] # and two more digits )? # But the decimal part is optional $ # And that's all there is to be /x) { } elsif { die "'$price' dosen't look like a valid price.\n" };

Update: Please do note that my guessed definition of "number" differs at least from BUUs definition of "number" below. It's not hard to come up with a solution that includes BUUs definition as well, but personally, I think a number should ALWAYS start with a digit from the range 0 to 9.

To add a support for the weird definition which allows a number to start with a period, use the following RE :

/^([0-9]+(\.[0-9]{2})?)|(\.[0-9]{2})$/
perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web

In reply to Re: Checking for numbers and a period by Corion
in thread Checking for numbers and a period by andrew

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.