in reply to How to determine if something is numeric?
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.
Cheers - L~R#!/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; }
|
|---|