Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   "is-numeric" check? (http://www.velocityreviews.com/forums/t948670-is-numeric-check.html)

Ivan Shmakov 07-29-2012 08:16 AM

"is-numeric" check?
 
Is there a simple way to check if a value is numeric in Perl?

FWIW, (($x + 0) eq $x) doesn't fit, as it returns false should
$x contain any leading zeros or whitespace.

Apparently, there /is/ such a check in Perl, but is there a way
to call it explicitly?

TIA.

$ perl -we 'print ("non-numeric" + 0, "\n");'
Argument "non-numeric" isn't numeric in addition (+) at -e line 1.
0
$

--
FSF associate member #7257 http://sf-day.org/

Wolf Behrenhoff 07-29-2012 08:41 AM

Re: "is-numeric" check?
 
Am 29.07.2012 10:16, schrieb Ivan Shmakov:
> Is there a simple way to check if a value is numeric in Perl?


Maybe you want:

use Scalar::Util qw(looks_like_number);

- Wolf


Ivan Shmakov 07-29-2012 09:19 AM

Re: "is-numeric" check?
 
>>>>> Wolf Behrenhoff <NoSpamPleaseButThisIsValid3@gmx.net> writes:
>>>>> Am 29.07.2012 10:16, schrieb Ivan Shmakov:


>> Is there a simple way to check if a value is numeric in Perl?


> Maybe you want:


> use Scalar::Util qw(looks_like_number);


Indeed, and it even seems to differentiate between integers,
floats and "specials" (which is what I need), like:

$ perl -we 'use Scalar::Util qw (looks_like_number);
foreach my $v (" 12", "abc", " 03", "4.5", ".67", "NaN") {
printf ("%3d %s\n", looks_like_number ($v), $v);
};'
1 12
0 abc
1 03
5 4.5
5 .67
36 NaN
$

Unfortunately, perlapi(3perl) [1] doesn't seem to describe the
exact meaning of these distinct values, so I guess I shouldn't
rely on that.

[1] http://perldoc.perl.org/perlapi.html#looks_like_number

--
FSF associate member #7257 http://sf-day.org/

Jürgen Exner 07-29-2012 10:46 AM

Re: "is-numeric" check?
 
Ivan Shmakov <oneingray@gmail.com> wrote:
> Is there a simple way to check if a value is numeric in Perl?


At some time this Question was Asked Frequently. Please see
"perldoc -q number":
"How do I determine whether a scalar is a
number/whole/integer/float?"

jue

Ivan Shmakov 07-30-2012 05:37 AM

Re: "is-numeric" check?
 
>>>>> Ben Morrow <ben@morrow.me.uk> writes:
>>>>> Quoth Ivan Shmakov <oneingray@gmail.com>:


>> Indeed, and it even seems to differentiate between integers, floats
>> and "specials" (which is what I need), like:


[...]

>> Unfortunately, perlapi(3perl) [1] doesn't seem to describe the exact
>> meaning of these distinct values, so I guess I shouldn't rely on
>> that.


> They are documented under grok_number, which is called by lln for
> scalars which are currently strings. This isn't reliable, though,
> because scalars which are currently numbers will return something
> entirely different:


[...]

> (Scalar::Util really ought to smash its return value to boolean.)


ACK, thanks. I've decided that I don't actually need to
distinguish integers from non-integers, and for now ended up
with the following bit of code:

sub number_or {
foreach my $v (@_) {
## .
return $v
if (looks_like_number ($v));
}

## .
undef;
}

## FIXME: should signal an error if FOO exists, non-empty and non-number
my $foo
= number_or ($ENV{"FOO"},
$ENV{"FOO_COMPAT"},
$foo_default);

--
FSF associate member #7257 http://sf-day.org/

Ivan Shmakov 07-30-2012 05:57 AM

Re: "is-numeric" check?
 
>>>>> Jürgen Exner <jurgenex@hotmail.com> writes:
>>>>> Ivan Shmakov <oneingray@gmail.com> wrote:


BTW, the From: of the article I'm replying to contains unencoded
(as per RFC 2047) non-ASCII data, which is explicitly prohibited
by the recent revision of the Netnews article format (RFC 5536,
section 2.2.)

The interoperability is thus non-warranted.

(In particular, I'm planning to work on a "NNTP server"
implementation next year, and it's likely that it will reject
messages with non-ASCII headers outright.)

>> Is there a simple way to check if a value is numeric in Perl?


> At some time this Question was Asked Frequently. Please see "perldoc
> -q number":


> "How do I determine whether a scalar is a
> number/whole/integer/float?"


ACK, thanks. It mentions looks_like_number, too, but also
POSIX::strtod, POSIX::strtol (which are somewhat non-portable,
AIUI); and pure-Perl String::Scanf and a solution using "given",
both based on Perl regular expressions, which I'd like to avoid.

(JFTR: it's also at [1].)

[1] http://perldoc.perl.org/perlfaq4.html

--
FSF associate member #7257 http://sf-day.org/

Dr.Ruud 07-30-2012 09:22 AM

Re: "is-numeric" check?
 
On 2012-07-29 10:16, Ivan Shmakov wrote:

> Is there a simple way to check if a value is numeric in Perl?


No. Why do you think that you need it?

Perl is a strongly typed language. The type is not in the values, as it
is in many other languages, but in the operators.

--
Ruud



Ivan Shmakov 07-30-2012 10:06 AM

Re: "is-numeric" check?
 
>>>>> Ruud <rvtol+usenet@xs4all.nl> writes:
>>>>> On 2012-07-29 10:16, Ivan Shmakov wrote:


>> Is there a simple way to check if a value is numeric in Perl?


> No. Why do you think that you need it?


My program receives a crucial piece of information via its
command line, and I'd like it to fail with a clear error message
should a non-number be passed, instead of silently (or with a
warning) interpreting it as zero.

> Perl is a strongly typed language.


I guess that my $x = "x" + 1; should then die at once.

> The type is not in the values, as it is in many other languages, but
> in the operators.


--
FSF associate member #7257 http://sf-day.org/

Dr.Ruud 07-30-2012 12:24 PM

Re: "is-numeric" check?
 
On 2012-07-30 12:06, Ivan Shmakov wrote:
> Ruud <rvtol+usenet@xs4all.nl> writes:
>> On 2012-07-29 10:16, Ivan Shmakov wrote:


>>> Is there a simple way to check if a value is numeric in Perl?

>>
>> No. Why do you think that you need it?

>
> My program receives a crucial piece of information via its
> command line, and I'd like it to fail with a clear error message
> should a non-number be passed, instead of silently (or with a
> warning) interpreting it as zero.


So you need to validate user input. For that you need to use a parser.
How do you define 'numeric'? Does 1_000_000 == 1000000?


>> Perl is a strongly typed language.

>
> I guess that my $x = "x" + 1; should then die at once.


perl -Mstrict -wle'
#local $SIG{"__WARN__"}= sub { die @_ };
my $x= "x" + 1;
print $x;
'

If you need it to die on warnings, uncomment that SIG line.


--
Ruud



Rainer Weikusat 07-30-2012 01:18 PM

Re: "is-numeric" check?
 
"Dr.Ruud" <rvtol+usenet@xs4all.nl> writes:
> On 2012-07-30 12:06, Ivan Shmakov wrote:


[...]

>>> Perl is a strongly typed language.

>>
>> I guess that my $x = "x" + 1; should then die at once.

>
> perl -Mstrict -wle'
> #local $SIG{"__WARN__"}= sub { die @_ };
> my $x= "x" + 1;
> print $x;
> '
>
> If you need it to die on warnings, uncomment that SIG line.


A strongly-typed language would be one where no automatic conversions
are performed, especially not very likely wrong ones like converting
"x" to 0. But by default, Perl doesn't even warn about this
conversion, this has to be enabled explicitly and that explicit code
needs to be written in order to turn this optional warning into a
fatal runtime error is also the opposited of 'strongly typed'.



All times are GMT. The time now is 01:29 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.