in reply to Re: decimal numbers as hash keys
in thread decimal numbers as hash keys

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?

Replies are listed 'Best First'.
Re^3: decimal numbers as hash keys
by kennethk (Abbot) on Jun 26, 2013 at 18:11 UTC
    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

    $jour=$ifac{0+$factor};
    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.