Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

0 views
Skip to first unread message

David Canzi

unread,
Oct 11, 2010, 6:31:25 PM10/11/10
to perlfaq...@perl.org
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.

brian d foy

unread,
Oct 13, 2010, 11:27:37 PM10/13/10
to perlfaq...@perl.org, David Canzi
[[ This message was both posted and mailed: see
the "To," "Cc," and "Newsgroups" headers for details. ]]

In article <4CB3903...@uwaterloo.ca>, David Canzi
<dmc...@uwaterloo.ca> wrote:

> Suggested change to simplify some regular expressions:

I used this opportunity to also convert the example to given-when. The
change is a13ded5516803d9baff49d9177619361dcc9d083.

0 new messages