On Thu, 18 Oct 2001 at 18:33:53 -0400, Selector, Lev Y wrote:
> Wow! This is really elegant !!

> -----Original Message-----
> From: Josh Goldberg [mailto:[EMAIL PROTECTED]]

> sub isNumber {
>         !/\d/ ? 0 : $_ == 0 ? 1 : $_ * 1
> }

Possibly elegant, but definitely not correct.

Anything non-numeric, but with digits in, will generate an ugly warning
and incorrectly return "1". Try 'foo0' and you'll see what I mean.
You did try the -w switch, didn't you?

I prefer Lev's original, and offer this as an improvement:

sub isNumber($){
    $_[0] =~ /^(?=[-+.]*\d)[-+]?\d*\.?\d*(?:e[-+ ]?\d+)?$/i
}

The initial zero-width assertion is to prevent ".e5" from being a
number, while permitting "123.".

The space after the "e" is in deference to FORTRAN, which writes numbers
like "1.23E 01". But that's a matter of taste.

I feel sure this is a FAQ...

Ian

Reply via email to