Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Re: Is it a number?

by broquaint (Abbot)
on Nov 12, 2002 at 00:19 UTC ( #212159=note: print w/ replies, xml ) Need Help??


in reply to Re: Is it a number?
in thread Does it look like a number?

Ah yes, that would be

sub is_numeric { ($_[0] & ~ $_[0]) eq "0"; }
Which tests whether a scalar is a numeric value or a string value e.g 42 vs "42". Whereas the snippet discerns whether a given scalar looks like a number or not e.g
print +(isnum($_) ? "is a number" : "not a number"), ": $_", $/ for qw( 1 2.2 3e3 4. .5); __output__ is a number: 1 is a number: 2.2 is a number: 3e3 is a number: 4. is a number: .5

HTH

_________
broquaint


Comment on Re: Re: Is it a number?
Select or Download Code
•Re: Re: Re: Is it a number?
by merlyn (Sage) on Oct 23, 2003 at 14:50 UTC
    Yeah, the difference is the difference between "could this string be converted into a number without getting the warnings if -w is on?" vs "is this value always and only a number?". Everything in the latter case is also true for the former, but the inverse is not true. For example, all external data read from filehandles is always a string initially, so it could never pass this "is_numeric" test.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://212159]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2014-11-24 04:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (135 votes), past polls