Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: check if its a number

by snoopy (Curate)
on Apr 12, 2008 at 00:33 UTC ( [id://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

Replies are listed 'Best First'.
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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://679901]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-19 06:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found