in reply to How to check Inputs if Numeric

That's an FAQ actually, as you can find browsing the output of perldoc -q number:

How do I determine whether a scalar is a number/whole/integer/f +loat? + Assuming that you don't care about IEEE notations like "NaN" or + "Infin- ity", you probably just want to use a regular expression. + if (/\D/) { print "has nondigits\n" } if (/^\d+$/) { print "is a whole number\n" } if (/^-?\d+$/) { print "is an integer\n" } if (/^[+-]?\d+$/) { print "is a +/- integer\n" } if (/^-?\d+\.?\d*$/) { print "is a real number\n" } if (/^-?(?:\d+(?:\.\d*)?|\.\d+)$/) { print "is a decimal num +ber\n" } if (/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/) { print "a C float\n" } + There are also some commonly used modules for the task. Scalar +::Util (distributed with 5.8) provides access to perl's internal funct +ion "looks_like_number" for determining whether a variable looks li +ke a number. Data::Types exports functions that validate data types + using both the above and other regular expressions. Thirdly, there is + "Reg- exp::Common" which has regular expressions to match various typ +es of numbers. Those three modules are available from the CPAN. + If you're on a POSIX system, Perl supports the "POSIX::strtod" +func- tion. Its semantics are somewhat cumbersome, so here's a "getn +um" wrapper function for more convenient access. This function tak +es a string and returns the number it found, or "undef" for input th +at isn't a C float. The "is_numeric" function is a front end to "getnum +" if you just want to say, ``Is this a float?'' + sub getnum { use POSIX qw(strtod); my $str = shift; $str =~ s/^\s+//; $str =~ s/\s+$//; $! = 0; my($num, $unparsed) = strtod($str); if (($str eq '') || ($unparsed != 0) || $!) { return undef; } else { return $num; } } sub is_numeric { defined getnum($_[0]) } Or you could check out the String::Scanf module on the CPAN ins +tead. The POSIX module (part of the standard Perl distribution) provi +des the "strtod" and "strtol" for converting strings to double and long +s, respectively.

I had your same problem, too :-)

Ciao!
--bronto


The very nature of Perl to be like natural language--inconsistant and full of dwim and special cases--makes it impossible to know it all without simply memorizing the documentation (which is not complete or totally correct anyway).
--John M. Dlugosz

Replies are listed 'Best First'.
Re: Re: How to check Inputs if Numeric
by kasmot (Novice) on Apr 01, 2004 at 03:14 UTC
    Thanks to all. You guys here were really very good and of great help.. More Power..

      I know this thread is old but it came up on google firstish. I searched, then came up with my own line, which I think works better than the regexp. I did not check all over, so most likely it is already around somewhere.

      if ( $input+0 eq $input ){ print "it is a number!\n";}

      Can someone point out the problem with this approach? Advantage for me: easy + quick, no problems with signs...

        Depends on whether you'd consider "000" a number, for example. (I would, probably, in most cases.)

        I reckon we are the only monastery ever to have a dungeon stuffed with 16,000 zombies.

        Not bad, especially given the simplicity. More potential false negatives to go with jdporter's.

        for my $input (qw< +4 1.0 1e5 12345678901234567890 1.234567890123456789 >) { if( $input+0 eq $input ) { print "$input is a number!\n"; } else { print "$input is not ", 0+$input, "\n"; } } __END__ +4 is not 4 1.0 is not 1 1e5 is not 100000 12345678901234567890 is not 1.23456789012346e+019 1.234567890123456789 is not 1.23456789012346

        Update: It successfully detects if $input is (primarily) holding a numeric value (an IV or NV, how Perl stores an integer and floating point, respectively) or is holding a string value that equals the canonical string representation of any possible Perl numeric value.

        So I don't believe that there are any false positives. The false negatives are the many ways to reasonably represent a numeric value that aren't Perl's canonical representation.

        - tye        

        just a quick (final) note : ($nr+0 eq $nr ) is not perfect for checking on numbers in general, but it is a very good check for integer numbers.

        It avoids perl complaining about casting as some other methods would and is pretty easy on resources

        If you don't mind "nan" and "inf" among numbers...
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

        thanks for the replies!

        some of the problems pointed out would not bother me much IRL ( i.e. most of my applications ), the one with 1.0 for example would. Back to regexp's I suppose :/