Quantcast
Channel: perl.perlfaq.workers
Viewing all articles
Browse latest Browse all 40

FAQ 4.72 How do I determine whether a scalar is a number/whole/integer/float? by David Canzi

$
0
0
Suggested change to simplify some regular expressions:

*** FAQ_4.72_before Mon Oct 11 18:07:09 2010
--- FAQ_4.72_after Mon Oct 11 18:09:57 2010
***************
*** 9,13 ****
if (/^[+-]?\d+$/) { print "is a +/- integer\n" }
! if (/^-?\d+\.?\d*$/) { print "is a real number\n" }
! if (/^-?(?:\d+(?:\.\d*)?|\.\d+)$/) { print "is a decimal
number\n" }
! if (/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/)
{ print "a C float\n" }
--- 9,12 ----
if (/^[+-]?\d+$/) { print "is a +/- integer\n" }
! if (/^-?(?:\d+\.?|\.\d)\d*$/) { print "is a decimal
number\n" }
! if (/^[+-]?(?=\.?\d)\d*\.?\d*(e[+-]?\d+)?$/i)
{ print "a C float\n" }

The regex /^-?\d+\.?\d*$/ is deleted because it doesn't match
numeric strings like '.5' with no digits before the decimal point,
and is otherwise equivalent to the regex on the next line. The
other two regexes are simplified.


Viewing all articles
Browse latest Browse all 40

Trending Articles