Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: decimal numbers as hash keys

by kennethk (Abbot)
on Jun 26, 2013 at 15:27 UTC ( #1040804=note: print w/replies, xml ) Need Help??

in reply to decimal numbers as hash keys

If you need a particular format of your number in order to get a desired behavior, you need to control how your number gets converted into a string. This is usually done using sprintf. For example
$ifac{sprintf "%.3f" $factor}
will cause the key accessed to be a floating point number with 3 decimal places, thus forcing the numbers to yield appropriate strings. It also avoids the potential 6.269999999999999999 problem.

In your case, your issue begins with your hash creation:

use Data::Dumper; my %ifac= ( 14.152 => "JCI", 9.334 => "JHEP", 5.745 => "JIMM", 6.270 => "JID", 1.480 => "JRN", 6.105 => "KI" ); print Dumper \%ifac;
$VAR1 = { '6.105' => 'KI', '1.48' => 'JRN', '9.334' => 'JHEP', '5.745' => 'JIMM', '6.27' => 'JID', '14.152' => 'JCI' };
You need to control string format on input, which means using strings not numbers for initialization:
my %ifac= ( '14.152' => "JCI", '9.334' => "JHEP", '5.745' => "JIMM", '6.270' => "JID", '1.480' => "JRN", '6.105' => "KI" );

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Replies are listed 'Best First'.
Re^2: decimal numbers as hash keys
by Alex31 (Initiate) on Jun 26, 2013 at 15:53 UTC

    Thank you for your fast reply.

    So even if it would work with a direct access, it is always better to translate the hash keys to a string.

    Just an idea: Another solution would be to cut the last 0 from $factor - which should then also work?

      The hash key is a string. If you don't do explicit stringification, Perl does it for you. In your case, it does it inconsistently between your construction and access.

      You would get identical functionality by trimming trailing zeroes (s/0+$//), but I think that behavior is less obvious, in which case you are setting yourself up for painful maintenance. Another option would be to multiply your factors by 1000, and thus deal with integers instead of decimals.

      Update: An improvement on the "trim trailing zeroes" front would be to use the same mechanism that mangled the input in the first place. If you swap to

      you'll invoke the internal Perl conversion to a number and reconvert back to a string for hash access, and hence should hit the correct record.

      #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1040804]
and John Coltrane plays...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2018-02-21 01:58 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (274 votes). Check out past polls.