Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: check if its a number

by snoopy (Deacon)
on Apr 12, 2008 at 00:33 UTC ( #679901=note: print w/ replies, xml ) Need Help??


in reply to check if its a number

Scalar::Util's looks_like_number method will do what you want.

If you need to distinguish integers from float; see also Scalar::Util::Numeric


Comment on Re: check if its a number
Re^2: check if its a number
by oko1 (Deacon) on Apr 12, 2008 at 02:22 UTC
    > Scalar::Util's looks_like_number method will do what you want.
    

    Scalar::Util uses a regex to get the answer. I'm not sure why snoopy's request has that requirement, but...

    ben@Tyr:~$ perl -wne'print if /sub looks_like_number/../}/' `perldoc - +l Scalar::Util` sub looks_like_number { local $_ = shift; # checks from perlfaq4 return 0 if !defined($_) or ref($_); return 1 if (/^[+-]?\d+$/); # is a +/- integer return 1 if (/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/); # +a C float return 1 if ($] >= 5.008 and /^(Inf(inity)?|NaN)$/i) or ($] >= 5.006 +001 and /^Inf$/i); 0; }
    
    -- 
    Human history becomes more and more a race between education and catastrophe. -- HG Wells
    

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (14)
As of 2014-07-30 14:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (234 votes), past polls