Just as there are different operators for comparing strings and numbers, you should be aware how that affects accessing elements of hashes.

Consider the following:

```my \$key1 = '00001';
my \$key2 = 1;
my %hash1 = (1 => 1);
my %hash2 = (1 ,  2);
my %hash3;
\$hash3{\$key1} = 3;
my %hash4;
\$hash4{\$key2} = 4;
Obviously, \$key1 == \$key2, but \$key1 ne \$key2. That is, they are equal as numbers, but since \$key1 is a string and \$key2 is not, then they will not match using the eq string operator. This leads to the following:

```00001 == 1       That is: \$key1 == \$key2
00001 ne 1       That is: \$key1 ne \$key2

\$hash1{\$key1} is undef
\$hash1{\$key2} == 1
\$hash2{\$key1} is undef
\$hash2{\$key2} == 2
\$hash3{\$key1} == 3
\$hash3{\$key2} is undef
\$hash4{\$key1} is undef
\$hash4{\$key2} == 4
This is particularly important when working with databases, for example, where the key field may be zerofilled.

Replies are listed 'Best First'.
Re: Hash Keys (strings or numbers?)
by petral (Curate) on Nov 15, 2001 at 03:29 UTC
Leading zero's can turn octal on you:
```\$ perl -lwe 'print shift() + 4' 012
16
\$ perl -lwe 'print 012 + 4'
14
\$ perl -lwe'print "012" + 4'
16
\$