Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Detecting if a scalar has a number or string

by ysth (Canon)
on Nov 18, 2004 at 21:15 UTC ( [id://408889]=note: print w/replies, xml ) Need Help??


in reply to Detecting if a scalar has a number or string

According to the perlfunc manpage on sort, I should be able to use <=> to compare things, and if it's NaN, it turns out undefined. I think the page is outdated. The following turns out true in ActiveState Perl Build 810 (Perl 5.8.4):

$a = 'string'; if (defined ($a <=> $a)) { print "Oops"; }

The == operator also seems to work for NaNs (Not-a-Numbers) when it's not supposed to.

I think you are under a misapprehension. $a there is not a NaN; a NaN is a floating point value that doesn't correspond to an actual number. On systems that support NaNs and infinite numbers, there are various rules about when a math operation will return one of these types. Try:
my $Inf = 9**9**9; my $NaN = sin($Inf); print "inf is: $Inf\n", "NaN is: $NaN\n";
Note: these are not strings; setting $a = "Inf" is not the same as setting it to an Inf floating point value.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-24 01:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found