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