FAQ 4.73 How do I determine whether a scalar is a number/whole/integer/float?


P

PerlFAQ Server

This is an excerpt from the latest version perlfaq4.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

--------------------------------------------------------------------

4.73: How do I determine whether a scalar is a number/whole/integer/float?

Assuming that you don't care about IEEE notations like "NaN" or
"Infinity", you probably just want to use a regular expression:

use 5.010;

given( $number ) {
when( /\D/ )
{ say "\thas nondigits"; continue }
when( /^\d+\z/ )
{ say "\tis a whole number"; continue }
when( /^-?\d+\z/ )
{ say "\tis an integer"; continue }
when( /^[+-]?\d+\z/ )
{ say "\tis a +/- integer"; continue }
when( /^-?(?:\d+\.?|\.\d)\d*\z/ )
{ say "\tis a real number"; continue }
when( /^[+-]?(?=\.?\d)\d*\.?\d*(?:e[+-]?\d+)?\z/i)
{ say "\tis a C float" }
}

There are also some commonly used modules for the task. Scalar::Util
(distributed with 5.8) provides access to perl's internal function
"looks_like_number" for determining whether a variable looks like a
number. Data::Types exports functions that validate data types using
both the above and other regular expressions. Thirdly, there is
"Regexp::Common" which has regular expressions to match various types of
numbers. Those three modules are available from the CPAN.

If you're on a POSIX system, Perl supports the "POSIX::strtod" function.
Its semantics are somewhat cumbersome, so here's a "getnum" wrapper
function for more convenient access. This function takes a string and
returns the number it found, or "undef" for input that 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 instead. The
"POSIX" module (part of the standard Perl distribution) provides the
"strtod" and "strtol" for converting strings to double and longs,
respectively.



--------------------------------------------------------------------

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top