Home > Archive > PERL Beginners > August 2005 > is there isnumber() function in perl?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
is there isnumber() function in perl?
|
|
| Jayvee Vibar 2005-08-25, 3:55 am |
| Hello,
Is there an IsNumber() function in perl? I'm looking for a way to determine
of a given $variable is numeric or not. Thanks.
| |
| Hot Flame 2005-08-25, 7:59 am |
| Use regex..
($variable =3D~ /^\d+$/)?1:0;
On 8/25/05, Jayvee Vibar <jcnvibar@hotmail.com> wrote:
> Hello,
>=20
> Is there an IsNumber() function in perl? I'm looking for a way to determi=
ne
> of a given $variable is numeric or not. Thanks.
>=20
>=20
>=20
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>=20
>=20
>
| |
| Xavier Noria 2005-08-25, 7:59 am |
| On Aug 25, 2005, at 3:22, Jayvee Vibar wrote:
> Is there an IsNumber() function in perl? I'm looking for a way to
> determine
> of a given $variable is numeric or not. Thanks.
If you need to know whether it looks like a number there are a few
ways, see
perldoc -q determine
If on the other hand what you need is a way to know whether the
underlying type is really numeric (for instance because you are going
to use it as operand of & on variables that might come as strings
("&" has a dual nature)) then normally one just enforces it explicitly:
sub subroutine_whose_args_are_integers_but_m
ight_come_as_strings {
my ($x, $y) = @_;
$x = 0 + $x; # ensure $x has underlying integer type
$y = 0 + $y; # ensure $y has underlying integer type
my $z = $x & $y; # safe, & acting as bitwise AND for sure
# ...
}
-- fxn
| |
| Charles K. Clarkson 2005-08-25, 7:59 am |
| Jayvee Vibar <mailto:jcnvibar@hotmail.com> wrote:
: Is there an IsNumber() function in perl? I'm looking for a way to
: determine of a given $variable is numeric or not. Thanks.
Read perlfaq4: "How do I determine whether a scalar is a
number/whole/integer/float?"
HTH,
Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328
|
|
|
|
|