Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Does it look like a number?

by broquaint (Abbot)
on Nov 11, 2002 at 19:42 UTC ( [id://212058]=CUFP: print w/replies, xml ) Need Help??

Ask perl if a given scalar looks like a number (requires Inline::C).

Inspired by Slaven Rezic's "Include Perl_looks_like_number in Scalar::Util?" post to p5p.

Hopefully this function, or something very similar to it, should be making it into Scalar::Util per the p5p thread.

update: changed the title (was Is it a number?) as it was a little ambiguous in relation to the code

use Inline C; my $var = shift; print +(isnum($var) ? "is a number" : "not a number"), ": $var", $/; __END__ __C__ int isnum(SV* val) { return Perl_looks_like_number(val); }

Replies are listed 'Best First'.
Re: Is it a number?
by princepawn (Parson) on Nov 11, 2002 at 20:48 UTC
    I remember in "Effective Perl Programming" merlyn did something like this:
    $_ && bit_flip $_ eq "0"
    Where bit_flip is something I forgot how to do in Perl.

    Carter's compass: I know I'm on the right track when by deleting something, I'm adding functionality

      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

        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.

Re: Does it look like a number?
by xdg (Monsignor) on Dec 15, 2004 at 17:16 UTC

    Lots more on this topic came up recently. See Detecting if a scalar has a number or string, which includes a lot of discussion about how to treat objects that overload like numbers. I personally like Re^3: Detecting if a scalar has a number or string by davido, which ducks a lot of the issues and tests to see if a given input would generate a warning if used in arithmetic -- which is what I usually find myself wanting to check when I'm being given user input (as opposed to whether something is represented internally as a number or string).

    -xdg

    Code posted by xdg on PerlMonks is public domain. It has no warranties, express or implied. Posted code may not have been tested. Use at your own risk.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-03-28 07:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found