Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: How to determine if something is numeric?

by Limbic~Region (Chancellor)
on Jan 30, 2004 at 19:28 UTC ( [id://325348]=note: print w/replies, xml ) Need Help??


in reply to How to determine if something is numeric?

mifflin,
You haven't done a very good job of explaining exactly what you mean by numeric. Are you only looking for integers or do decimals count? You say no alphas, but is a larger integer represented in scientific notation acceptable? What about Inf and NaN? I would suggest having a look at Scalar::Util's looks_like_number but that doesn't seem to be what you want.

If you are looking for positive or negative whole integers that do not have any leading zeros, the following should work. Remember 0 by itself will not be numeric.

#!/usr/bin/perl -w use strict; my $test = -413; print "$test is ok\n" if numeric( $test ); sub numeric { my $number = shift; return 0 if ! $number; my $first = substr( $number , 0 , 1 ); $number = substr($number, 1) if $first eq '-'; return 1 if $number =~ /^[1-9]\d+?$/; return 0; }
Cheers - L~R

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://325348]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-25 10:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found