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

Re: Dangerous Names

by ccn (Vicar)
on Dec 17, 2008 at 21:55 UTC ( [id://731067]=note: print w/replies, xml ) Need Help??


in reply to Dangerous Names

Perl wants to see numbers around <=> (spaceship) operator and converts strings to numbers. "Nan" and "NaN" are both converted to zero.

Update: Strings which start with "nan" (/^nan/i) are converted to nan, others which start with "inf" (/^inf/i) converted to inf. Others are converted to zero unless begin with digits.

see perldoc peldata (Scalar values)

#!/usr/bin/perl -l -- use strict; use warnings; my @s =qw(nan naNo inf InfInity infimum foo bar 123dd); print "$_ => ", $_+0 for @s; __END__ Argument "naNo" isn't numeric in addition (+) at a.pl line 7. Argument "infimum" isn't numeric in addition (+) at a.pl line 7. Argument "foo" isn't numeric in addition (+) at a.pl line 7. Argument "bar" isn't numeric in addition (+) at a.pl line 7. Argument "123dd" isn't numeric in addition (+) at a.pl line 7. nan => nan naNo => nan inf => inf InfInity => inf infimum => inf foo => 0 bar => 0 123dd => 123

Replies are listed 'Best First'.
Re^2: Dangerous Names
by samtregar (Abbot) on Dec 17, 2008 at 21:58 UTC
    Incorrect! Proof:

    $ perl -e '@names = (["Nan Tregar"], ["Sam Tregar"]); @names = sort { +$a->[0] <=> $b->[0] } @names;' Sort subroutine didn't return a numeric value at -e line 1.

    Note that you have to work a little. Just a straight { $a <=> $b } gets optimized into something that doesn't do the NaN conversion.

    -sam

      I didn't need to do any extra work. See my post below.
      $ perl -v This is perl, v5.8.8 built for x86_64-linux-gnu-thread-multi
        Ok - but do you find it unusual that sort { $a <=> $b } doesn't die, but sort { $a->[0] <=> $b->[0] } does?

        -sam

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-25 18:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found