Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] Could arrays be used as hash keys?

Expand Messages
  • Jenda Krynicky
    To: perl-beginner@yahoogroups.com From: aditi gupta Date sent: Tue, 11 Mar 2008 22:28:11 +0000 (GMT)
    Message 1 of 7 , Mar 12 7:04 AM
    • 0 Attachment
      To: perl-beginner@yahoogroups.com
      From: aditi gupta <aditi9783@...>
      Date sent: Tue, 11 Mar 2008 22:28:11 +0000 (GMT)
      Subject: [PBML] Could arrays be used as hash keys?
      Send reply to: perl-beginner@yahoogroups.com

      > Hello All:
      >
      > I was wondering whether it is possible to have arrays as hash keys. For example:
      >
      > %hash = { ["a", "b"] => ["c", "d"] };
      >
      > I came across hash slices while searching for this, but they seem to map individual keys to individual values.
      >
      > I'd appreciate any help in this regard.
      > Thanks
      > Aditi

      A quick search on http://search.cpan.org reveals those:

      http://search.cpan.org/~osfameron/Tie-Hash-StructKeyed-
      0.03/lib/Tie/Hash/StructKeyed.pm

      http://search.cpan.org/~snowhare/Tie-ListKeyedHash-
      1.02/lib/Tie/ListKeyedHash.pod

      http://search.cpan.org/~nuffin/Tie-RefHash-1.38/lib/Tie/RefHash.pm

      Please keep in mind that
      [1,2] != [1,2]
      because each [] created a brand new reference to a new array!
      Which causes the Tie::Refhash to behave differently than
      Tie::ListKeyedHash or Tie::Hash::Structkeyed!

      Jenda
      ===== Jenda@... === http://Jenda.Krynicky.cz =====
      When it comes to wine, women and song, wizards are allowed
      to get drunk and croon as much as they like.
      -- Terry Pratchett in Sourcery
    • Alexander Saydakov
      ... Hashes (and arrays) can only hold scalars, that is, a string, a number or a reference. So in your example the hash will look like (if dumped with
      Message 2 of 7 , Mar 13 12:30 PM
      • 0 Attachment
        --- In perl-beginner@yahoogroups.com, aditi gupta <aditi9783@...> wrote:
        >
        > I was wondering whether it is possible to have arrays as hash keys.
        For example:
        >
        > %hash = { ["a", "b"] => ["c", "d"] };

        Hashes (and arrays) can only hold scalars, that is, a string, a number
        or a reference. So in your example the hash will look like (if dumped
        with Data::Dumper):

        $VAR1 = {
        'ARRAY(0x804e15c)' => [
        'c',
        'd'
        ]
        };

        Of course, that number is quite random, and, more importantly, if you
        tried to overwrite it like this:

        $hash{["a", "b"]} = 'something else';

        you would actually insert one more pair with some other random
        reference instead:

        $VAR1 = {
        'ARRAY(0x804e15c)' => [
        'c',
        'd'
        ],
        'ARRAY(0x809add4)' => 'something else'
        };


        /sandy http://myperlquiz.com/
      Your message has been successfully submitted and would be delivered to recipients shortly.