in reply to Adding hash values together

Hashes stringify keys, and uses string comparisons to compare keys. That means:
$hash{20} = 1; # Same as $hash{'20'} = 1; $hash{2e1} = 2; # Same as $hash{'20'} = 2; $hash{'2e1'} = 3; # Same as $hash{'2e1'} = 3; $hash{'2E1'} = 4; # Same as $hash{'2E1'} = 4; $hash{0+'2e1'} = 5; # Same as $hash{'20'} = 5; $hash{0+'2E1'} = 6; # Same as $hash{'20'} = 6; # '2E1' => 4, '2e1' => 3, '20' => 6

Update: Changed the wording in the comments to avoid further confusion.

Replies are listed 'Best First'.
Re: Adding hash values together
by MonkPaul (Friar) on Jun 30, 2005 at 18:06 UTC
    I dont really understand where you are coming from in your example: do you mean that the string literal can cause some over writing in the hash key or something completely different ???

    MonkPaul

      I meant that 20, 024, 0x14, 2e1 and 2E1 will all stringify to '20', and therefore are all equal when used as keys.

      However, '20', '024', '0x14', '2e1' and '2E1' are already strings, so they are all considered unequal if used as keys.