True enough however please don't be so rude, people should be free to
post the answers they think if only to be corrected and learn, and not
be scared off by know by flippant replies.

You are correct it will return a false positive as will $x=~/^[0-9]*$/
for that matter. But its an ultra simple solution for a simple question
I don't know if the person is loading from a file etc...


Even perldoc -q "whether a scalar is a number"

states

Assuming that you don't care about IEEE notations like "NaN"
     or "Infinity", you probably just want to use a regular
     expression.

        if (/\D/)            { print "has nondigits\n" }
        if (/^\d+$/)         { print "is a whole number\n" }
        if (/^-?\d+$/)       { print "is an integer\n" }
        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" }

Yes you can use

Scalar::Util but most people just want a simple test.



-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] 
Sent: 04 March 2008 21:45
To: beginners@perl.org
Subject: Re: variable help

>>>>> ""Andrew" == "Andrew Curry" <[EMAIL PROTECTED]> writes:

"Andrew> If you are not 100% sure then you can also do if ($x =~/^\d*$/)
for an
"Andrew> actual integer i.e. a number in the positive whole set of
numbers {
"Andrew> 1,2,3....}  or negative whole numbers -1,-2,-3.... or 0. 

This also fails, declaring "123\n" as a match, and thus being
"an integer".  False positive.

Please check your answers before answering on beginners next time.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777
0095
<[EMAIL PROTECTED]> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl
training!

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


This e-mail is from the PA Group.  For more information, see www.thepagroup.com.
This e-mail may contain confidential information.  
Only the addressee is permitted to read, copy, distribute or otherwise use this 
email or any attachments.  
If you have received it in error, please contact the sender immediately.  
Any opinion expressed in this e-mail is personal to the sender and may not 
reflect the opinion of the PA Group.
Any e-mail reply to this address may be subject to interception or monitoring 
for operational reasons or for lawful business practices.


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to