Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > "is-numeric" check?

Reply
Thread Tools

"is-numeric" check?

 
 
Ivan Shmakov
Guest
Posts: n/a
 
      07-29-2012
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/
 
Reply With Quote
 
 
 
 
Wolf Behrenhoff
Guest
Posts: n/a
 
      07-29-2012
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

 
Reply With Quote
 
 
 
 
Ivan Shmakov
Guest
Posts: n/a
 
      07-29-2012
>>>>> Wolf Behrenhoff <> 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/
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      07-29-2012
Ivan Shmakov <> 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
 
Reply With Quote
 
Ivan Shmakov
Guest
Posts: n/a
 
      07-30-2012
>>>>> Ben Morrow <> writes:
>>>>> Quoth Ivan Shmakov <>:


>> 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/
 
Reply With Quote
 
Ivan Shmakov
Guest
Posts: n/a
 
      07-30-2012
>>>>> Jürgen Exner <> writes:
>>>>> Ivan Shmakov <> 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/
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      07-30-2012
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


 
Reply With Quote
 
Ivan Shmakov
Guest
Posts: n/a
 
      07-30-2012
>>>>> Ruud <rvtol+> 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/
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      07-30-2012
On 2012-07-30 12:06, Ivan Shmakov wrote:
> Ruud <rvtol+> 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


 
Reply With Quote
 
Rainer Weikusat
Guest
Posts: n/a
 
      07-30-2012
"Dr.Ruud" <rvtol+> 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'.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57