[Chicago-talk] detecting whether a scalar is number or a string.

Andy Lester andy at petdance.com
Sun Jul 6 18:05:53 PDT 2008


On Jul 6, 2008, at 8:03 PM, Shawn Carroll wrote:

> Sorry, too quick to answer:
> $string =~ m/[[:digit:]]/;
> -or-
> $string =~ m/[[:alpha:]]/;
>

But that's incomplete because $string = "lsdkf3123" will be seen as a  
number, when it is not.

The low tech way to do it is

my $n = $string + 0;
if ( $n == $string ) {
     # then string is a number
}

This is all answered in the FAQ anyway.


--
Andy Lester => andy at petdance.com => www.petdance.com => AIM:petdance






More information about the Chicago-talk mailing list