List Info

Thread: numeric or alpha function




numeric or alpha function
country flaguser name
United States
2007-05-24 12:50:38

Does anyone know how to check for numeric or alpha on a scalar or does one have to check every character for /0..9/ or /a..z/ or /A..Z/? Is there a built-in function for this?

Bob

---------------------------------
Get the free Yahoo! toolbar and rest assured with the added security of spyware protection.

[Non-text portions of this message have been removed]

__._,_.___
.

__,_._,___
Re: numeric or alpha function
country flaguser name
United States
2007-05-24 13:19:01

#!/usr/bin/perl
use strict;
use warnings;
use Scalar::Util qw(looks_like_number);

my $var1 = '1'; # That's numeric
my $var2 = '1n'; # That's NOT numeric
my $var3 = '-1.05'; # That's numeric

if (looks_like_number($var1)) {
print "$var1 is a number!n";
}
else {
print "$var1 is NOT a number!n";
}

if (looks_like_number($var2)) {
print "$var2 is a number!n";
}
else {
print "$var2 is NOT a number!n";
}

if (looks_like_number($var3)) {
print "$var3 is a number!n";
}
else {
print "$var3 is NOT a number!n";
}

exit 0;

Results in...
1 is a number!
1n is NOT a number!
-1.05 is a number!

--- In perl-beginner%40yahoogroups.com">perl-beginneryahoogroups.com, Robert Brown <rdabman4...> wrote:
&gt;
> Does anyone know how to check for numeric or alpha on a scalar or
does one have to check every character for /0..9/ or /a..z/ or /A..Z/?
Is there a built-in function for this?
&gt;
> Bob
>
>
> ---------------------------------
> Get the free Yahoo! toolbar and rest assured with the added security
of spyware protection.
>
> [Non-text portions of this message have been removed]
>

__._,_.___
.

__,_._,___
Re: numeric or alpha function
country flaguser name
United States
2007-06-18 06:37:07

--- In perl-beginner%40yahoogroups.com">perl-beginneryahoogroups.com, Robert Brown <rdabman4...> wrote:
&gt;
> Does anyone know how to check for numeric or alpha on a scalar or
does one have to check every character for /0..9/ or /a..z/ or /A..Z/?
Is there a built-in function for this?
&gt;
> Bob

a regular expression, something like ^[a-zA-Z]+$ would help you to
check if the scalar is all alphabetic or not (the ^w+$ won't be useful
as w matches alphanumeric)

yogesh

__._,_.___
.

__,_._,___
[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )