XP is just a number PerlMonks

### Hash Keys (strings or numbers?)

by robot_tourist (Hermit)
 on Nov 14, 2001 at 14:55 UTC Need Help??

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;
[download]```
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
[download]```
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
\$
[download]```

p
Re: Hash Keys (strings or numbers?)
by ruzam (Curate) on Mar 02, 2012 at 14:20 UTC
When working with databases I've learned (through past mistakes) to always use '\$key += 0' on numeric field results to ensure they don't get quirky string representations.
Re: Hash Keys (strings or numbers?)
by cybear (Monk) on Jun 08, 2006 at 14:36 UTC
I see the difference and that is worth keeping in mind... but so what? How does that affect the speed or effectiveness of accessing hashes by number or string? Is there any real difference in the efficiency of string vs. number? That is not explored.

Re: Hash Keys (strings or numbers?)
by Artemus (Novice) on Mar 02, 2012 at 07:46 UTC
Unfortunately, none of the examples converts \$key1 into a number so that \$hash1{int(\$key1)} eq \$hash1{int(\$key2)}, when \$key1 == \$key2.

Log In?
 Username: Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perltutorial [id://125289]
help
Chatterbox?
 [davido]: Using the -i switch causes Perl to rename the input file, read from it, and write to a file using the original file's name. If there's an extension provided, as in -i.bak, it's easy to see where the input file is. Where is the input file temporarily.... [davido]: placed if there is no extension provided to the -i switch? [davido]: Nevermind, found the answer. [davido]: If no extension is supplied, and your system supports it, the original file is kept open without a name while the output is redirected to a new file with the original filename. When perl exits, cleanly or not, the original file is unlinked. [haukex]: doc says "If no extension is supplied, and your system supports it, the original file is kept open without a name ..." [Corion]: :)

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (14)
As of 2017-09-22 14:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
During the recent solar eclipse, I:

Results (264 votes). Check out past polls.

Notices?