Using strings as keys of hash

P

Peng Yu

Hi,

It seems that if strings are keys, the single quote can be omitted. I
don't see why on perl hash tutorial webpage such as
http://www.cs.mcgill.ca/~abatko/com...rl/howto/hash/#add_a_key_value_pair_to_a_hash

Can somebody give me a brief explanation why the single quote can be
omitted if the strings are used as the keys of a hash?

Thanks,
Peng

#!/usr/bin/perl

use strict;
use warnings;

my %hash = (
Fred => 'Flintstone',
Barney => 'Rubble'
);

# I can use the following code as well.
#my %hash = (
# 'Fred' => 'Flintstone',
# 'Barney' => 'Rubble'
#);

#The strings does not have single quotes as well.
print "$hash{Fred}\n";
print "$hash{Barney}\n";

print "$hash{'Fred'}\n";
print "$hash{'Barney'}\n";
 
M

Martien Verbruggen

Hi,

It seems that if strings are keys, the single quote can be omitted. I
don't see why on perl hash tutorial webpage such as
http://www.cs.mcgill.ca/~abatko/com...rl/howto/hash/#add_a_key_value_pair_to_a_hash

Can somebody give me a brief explanation why the single quote can be
omitted if the strings are used as the keys of a hash?

It really has nothing to do with strings being keys in a hash, and
everything to do with the => operator. The reason is simply that that
is how the => operator was designed.

$ perldoc perlop
[snip]
The "=>" operator is a synonym for the comma, but forces any word
(consisting entirely of word characters) to its left to be
interpreted as a string (as of 5.001). This includes words that
might otherwise be considered a constant or function call.
[snip]

Note that the => is simply a comma, except for the auto-quoting
behaviour of its left operand. It can be used in other places where a
comma would normally be used, such as subroutine calls.

my $foo = Foo->new(arg1 => 12, arg3 => 4);

is exactly equivalent to

my $foo = Foo->new("arg1", 12, "arg3", 4);

(or with single quotes if you prefer). There is no hash involved at any
time.

Martien
 
T

Tad J McClellan

Hash keys are *always* strings.

the single quote can be omitted. I
don't see why on perl hash tutorial webpage such as
http://www.cs.mcgill.ca/~abatko/com...rl/howto/hash/#add_a_key_value_pair_to_a_hash

Can somebody give me a brief explanation why the single quote can be
omitted if the strings are used as the keys of a hash?

It really has nothing to do with strings being keys in a hash, and
everything to do with the => operator. The reason is simply that that
is how the => operator was designed.

$ perldoc perlop
[snip]
The "=>" operator is a synonym for the comma, but forces any word
(consisting entirely of word characters) to its left to be
interpreted as a string (as of 5.001). This includes words that
might otherwise be considered a constant or function call.


Note also that hash keys that are all word chars do not need quoting,
even without the "fat comma" operator:

$hash{some_key}
and
$hash{'some_key'}
are the same thing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top