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

Richard Reina richard at rushlogistics.com
Mon Jul 7 04:36:39 PDT 2008


Well, It looked promising but,

#!/usr/bin/perl -w

my $string = "sdadada";

my $n = $string + 0;
if ( $n == $string ) {
     print"string is a number\n";
} else {
     print "string is a string\n";
}


Gives me:

Argument "sdadada" isn't numeric in addition (+) at test_string line 5.
string is a number



---- Chicago.pm chatter <chicago-talk at pm.org> wrote:
>
> 
> 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
> 
> 
> 
> 
> _______________________________________________
> Chicago-talk mailing list
> Chicago-talk at pm.org
> http://mail.pm.org/mailman/listinfo/chicago-talk
> 


More information about the Chicago-talk mailing list