For a long time I'm using the function below to test for a number, being
quite satisfied with it ...

####################################################
# (nmb) number
# Returns decimal value of the contents if argument
# is a number (integer if octal or hexadecimal),
# otherwise returns empty string ('')
####################################################
sub number($) {
    local $_ = shift; s/^\s*(.*)\s*$/$1/;
    return '' unless
/^[+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?$|^[+-]?0x[0-9a-fA-F]+$/;
    s/\+//; my $sign = s/^-// ? '-' : '';
    /^0[0-7]+$|^0x[0-9a-fA-F]+$/ ? $sign.oct : $sign.$_*1;
}

Greetings

 -- Andreas
______________________________________________________________________


Delphi Electronics & Safety FUBA Reception Systems

Andreas Kamentz
Electrical Engineer / SW
Antenna Engineering
[EMAIL PROTECTED]
Tel: (+49) 5063.990.541
Fax: (+49) 5063.990.99541

Mailing address
FUBA Automotive GmbH & Co. KG
TecCenter
D-31162 Bad Salzdetfurth / Germany

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of
Siebe Tolsma
Sent: Thursday, June 30, 2005 5:31 PM
To: Joe Discenza; [email protected]
Subject: Re: Test if string is a number?


How about regexp?

/^\-?(\d+\.?\d*|\.\d+)$/
----- Original Message -----
From: Joe Discenza
To: Chris Wagner ; perl-win32-users
Sent: Thursday, June 30, 2005 4:48 PM
Subject: RE: Test if string is a number?


Chris Wagner wrote, on Thu 6/30/2005 08:48

: Wow there's been a lot of heavy duty code proposed to do something so
: simple.  The answer is in how Perl converts between the two.
:
: print "is a number" if $var eq $var + 0;
: print "not a number" if $var ne $var + 0;
Except if $var is, say, '0.00'. Then $var + 0 is '0', and won't eq $var.

Joe
==============================================================
          Joseph P. Discenza, Sr. Programmer/Analyst
               mailto:[EMAIL PROTECTED]

          Carleton Inc.   http://www.carletoninc.com
          574.243.6040 ext. 300    fax: 574.243.6060

Providing Financial Solutions and Compliance for over 30 Years



_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

****************************************************************************************

Note: The information contained in this message may be privileged and 
confidential and thus protected from disclosure. If the reader of this message 
is not the intended recipient, or an employee or agent responsible for 
delivering this message to the intended recipient, you are hereby notified that 
any dissemination, distribution or copying of this communication is strictly 
prohibited. If you have received this communication in error, please notify us 
immediately by replying to the message and deleting it from your computer. 
Thank you.

****************************************************************************************

_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to