Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Checking whether a $var is a number

by dragonchild (Archbishop)
on Sep 05, 2001 at 00:24 UTC ( [id://110150]=note: print w/replies, xml ) Need Help??


in reply to Checking whether a $var is a number

The really obvious way to do it is to make sure that the scalar doesn't contain a non-numeric character.
if ($var && $var !~ /\D/) { # We have a value and it's a number }
You need the initial check to make sure that $var actually contains something, otherwise the !~ will trigger a warning about unitialized variables.

So, you can code up a neato little function, sorta like:

sub isNum { if ($var && $var !~ /\D/) { return 1; } else { return 0; } }
Then, put that into a module of yours and have that module inherit from Exporter and you're all good! :)

(Yes, I'm sure someone's already done this, but this was too cool an opportunity to promote module use. *blush*)

------
We are the carpenters and bricklayers of the Information Age.

Vote paco for President!

Replies are listed 'Best First'.
Re: Re: Checking whether a $var is a number
by wog (Curate) on Sep 05, 2001 at 00:32 UTC
    Unfortunatly checking for non-digits is not sufficient since these are all numbers:

    • 12.7
    • 1E0
    • 1.477e7

    Checking just for non-digits won't work on those. And you cannot just check for digits, E, e, and ., since these are all not numbers

    • ...
    • Eeeeeee.
    • 12e
    • e15
    • 1.6.8

    The only thing that your technique will work for is integers -- certainly not the only thing meant by 'number'.

      that's easy to fix (untested):
      qr/(?:+|-)?\d+(?:\.)?(?:\d+)?(?:[Ee](?:+|-)?\d+)/

      piece by piece:
      (?:+|-)? # Optional leading + or - \d+ # One or more digit (?:\.)? # A trailing . 12. is a number I think (?:\d+)? # The part after the decimal (?:[Ee](?:+|-)?\d+) # E or e followed by an optional + or - and one or + more digit
      I don't think I left anything out. Now, this won't match .4 as a number.... You can build a regex that will handle that case properly as well. But the simple fix of making the part before the decimal optional causes another bug, it maks every part optional, so the null string would be a match, and since every string starts with a null string, every string would match. Since all we have is a number, we could anchor to the begenning and end of the value, but then, the empty string would still match. Thinking back to his origonial question, this should be anchored to the front and back, so more like qr/^...$/.
      -Ted

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-25 13:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found