References as hash keys (Srinivasan's "Advanced Perl Programming")?

Discussion in 'Perl Misc' started by Adam Funk, Jun 27, 2006.

  1. Adam Funk

    Adam Funk Guest

    Srinivasan's "Advanced Perl Programming" (1997) says

    --> Perl requires hash keys to be strings, so when you use a
    --> reference as a key, Perl uses the reference's string
    --> representation.... But when you later retrieve the key from
    --> this hash, it will remain a string and will thus be unusable
    --> as a reference. It is possible that a future release of Perl
    --> may lift the restriction that hash keys have to be strings,
    --> but for the moment, the only recourse ... is to use the
    --> Tie::RefHash module.... There are few algorithms that
    --> require references to be used as hash keys and fewer still
    --> that cannot live with this restriction.

    Is this still (5.8.x) the case? What practical effect does this
    limitation have?
    Adam Funk, Jun 27, 2006
    #1
    1. Advertising

  2. Adam Funk

    Adam Funk Guest

    On 2006-06-27, Jim Gibson <> wrote:

    > The practical effect is that if you do want to use references as hash
    > keys, you can, but if you need to dereference the references, you can't
    > store the references only in the hash as keys unless you use the
    > Tie::RefHash module. Without using that module, you need to store the
    > references separately from the hash, in an array for example, and
    > dereference those values and use them as keys to the hash.
    >
    > Like the documents say, this is rarely necessary. I only contemplated
    > doing it once in many years of Perl programming, and, although it
    > worked, I soon found it unnecessary and abandoned it.


    Interesting; thanks.
    Adam Funk, Jun 28, 2006
    #2
    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. Radha Patel
    Replies:
    1
    Views:
    351
    Noah Roberts
    Apr 5, 2004
  2. rp
    Replies:
    1
    Views:
    511
    red floyd
    Nov 10, 2011
  3. Xeno Campanoli
    Replies:
    16
    Views:
    261
    Martin DeMello
    Aug 25, 2005
  4. Alex Fenton

    Hash#values and Hash#keys order

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

    hash.keys and hash.values

    Mage, Aug 13, 2006, in forum: Ruby
    Replies:
    14
    Views:
    173
Loading...

Share This Page