journey has asked for the wisdom of the Perl Monks concerning the following question:

Greetings, I built a hash of hashes with the data read from a text file. The keys are numeric such as "4.98 5.00 5.02 ..." and the values are counts. The result of any arithmetics performed on keys and values yields a result where the 5.00 key is undef (and only that one). Of course there are warnings of "use of uninitialized value in multiplication". I've tried to handle it various ways, but to no avail and it seems clean in the text file. Any tips on what is happening? You will have understood I am at the very beginning of my journey.... Thanks!

Replies are listed 'Best First'.
Re: Numeric as hash key returns undef...
by friedo (Prior) on Oct 24, 2006 at 21:15 UTC

    Are you certain that your hash keys are what you think they are? If you're doing floating-point math, you may end up with keys that have more decimal places. Even an integer like 5.00 might be stringifying as 5.00000001 if there's FP math involved.

    Try using Data::Dumper on your hash to see if that's the case.

      Oh yes, good catch :-)

      (to the OP) - hash keys are always regarded as strings - that means you should make sure your numeric keys are represented as exactly the right string. I.e. $hash{1.0} is not the same as $hash{1}*. One way of doing that is to use sprintf. For example, if all your keys have 2 places behind the comma, you can use something like this:

      my $key = sprintf("%0.2f",$number); my $value = $hash{$key}
      Actually, it is. But $hash{"1.0"} is not the same as $hash{1.0} go figure. :-)

        It actually gets weirder than that -- the . (concatenation) operator has higher precedence than a hash's auto-stringification. This confused a somewhat baffling bug for a few hours recently...
        use Data::Dumper; $x{a} = "bare a"; $x{"a.b"} = "quoted a.b"; $x{a.b} = "unquoted a.b"; print Dumper(\%x);' __DATA__ $VAR1 = { 'ab' => 'unquoted a.b', 'a.b' => 'quoted a.b', 'a' => 'bare a' };
        Thanks to all for your help.
        My mistake was effectively to have another hash where the keys were not quoted hence the "undef" for key 5.00 when multiplying. I hope I did not abuse of your time. Many thanks again.
Re: Numeric as hash key returns undef...
by Joost (Canon) on Oct 24, 2006 at 21:15 UTC
Re: Numeric as hash key returns undef...
by driver8 (Scribe) on Oct 25, 2006 at 02:01 UTC
    My guess would be that you are trying to get the value like this:
    $foo = $hash{5.00};
    which perl is interpreting as:
    $foo = $hash{5};
    What you want is this:
    $foo = $hash{'5.00'};
    To further illustrate, check what the following code does:
    $hash{5} = "foo"; $hash{'5.00'} = "bar"; print $hash{5}, $hash{5.00}, $hash{'5.00'};

    -driver8
Re: Numeric as hash key returns undef...
by ikegami (Patriarch) on Oct 24, 2006 at 21:24 UTC
    I'm don't know what you mean when you say you perform arithmetic on keys. Keys can't be changed, only created, deleted, enumerated and counted. Please show us minimal code that exhibits the problem.