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

Re: Numbers aren't averaging correctly

by chester (Hermit)
on Oct 06, 2005 at 02:26 UTC ( #497796=note: print w/ replies, xml ) Need Help??


in reply to Numbers aren't averaging correctly

Are you parsing out the commas first? Perl auto-converts strings into numbers, but it doesn't like non-alphanumeric characters and when it finds some it just makes a guess.

perl -le 'print "2,300" + "3,500"'

Output:

5


Comment on Re: Numbers aren't averaging correctly
Select or Download Code
Re^2: Numbers aren't averaging correctly
by revdiablo (Prior) on Oct 06, 2005 at 14:25 UTC

    It doesn't really just make a guess. It grabs all the numeric value it can find from the front of the string, and truncates anything past that. You can see the effect in action:

    my @strings = qw( 2,300 2.54xyz123 2e32abc3 2.34,23 ); for (@strings) { print "$_ => ", 0+$_, "\n"; }

    This prints:

    2,300 => 2 2.54xyz123 => 2.54 2e32abc3 => 2e+32 2.34,23 => 2.34

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (12)
As of 2014-12-21 17:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (106 votes), past polls