Using strings as keys of hash

Discussion in 'Perl Misc' started by Peng Yu, Nov 29, 2008.

  1. Peng Yu

    Peng Yu Guest

    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";
     
    Peng Yu, Nov 29, 2008
    #1
    1. Advertising

  2. On Sat, 29 Nov 2008 15:09:36 -0800 (PST),
    Peng Yu <> wrote:
    > 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
    --
    |
    Martien Verbruggen | The gene pool could use a little
    | chlorine.
    |
     
    Martien Verbruggen, Nov 29, 2008
    #2
    1. Advertising

  3. Martien Verbruggen <> wrote:
    > On Sat, 29 Nov 2008 15:09:36 -0800 (PST),
    > Peng Yu <> wrote:
    >> Hi,
    >>
    >> It seems that if strings are keys,



    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.


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, Nov 30, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rp
    Replies:
    1
    Views:
    543
    red floyd
    Nov 10, 2011
  2. Xeno Campanoli
    Replies:
    16
    Views:
    268
    Martin DeMello
    Aug 25, 2005
  3. Alex Fenton

    Hash#values and Hash#keys order

    Alex Fenton, Apr 7, 2006, in forum: Ruby
    Replies:
    1
    Views:
    145
    George Ogata
    Apr 15, 2006
  4. Mage

    hash.keys and hash.values

    Mage, Aug 13, 2006, in forum: Ruby
    Replies:
    14
    Views:
    183
  5. Ronald Fischer

    Hash#keys, Hash#values order question

    Ronald Fischer, Aug 23, 2007, in forum: Ruby
    Replies:
    0
    Views:
    156
    Ronald Fischer
    Aug 23, 2007
Loading...

Share This Page