Good question. Let's investigate.

# After this line from snippet 1... my %testHash = ( \@multiKey1 => \@stuff1, \@multiKey2 => \@stuff2, \@m +ultiKey3 => \@stuff3); # ... add this: use Data::Dumper; print Dumper \%testHash; # And run the snippet. __END__ Output: $VAR1 = { 'ARRAY(0x98a9b4)' => [ '6', '7' ], ... ...

Looks like the array ref for the key has been turned into a string. I'm a little foggy on the details, but from the top of my head I seem to remember that the => operator converts whatever is on it's left hand to a string. So here's an idea: let's not use the fat comma and see what happens next.

# Change this line from your first snippet... # my %testHash = ( \@multiKey1 => \@stuff1, \@multiKey2 => \@stuff2, \ +@multiKey3 => \@stuff3); # into this: my %testHash = ( \@multiKey1, \@stuff1, \@multiKey2, \@stuff2, \@mult +iKey3, \@stuff3); # ... and still use this: use Data::Dumper; print Dumper \%testHash; # And run the code.

Gah, no luck. Output remains the same. So it isn't the fat comma's fault. Of course, it could still be that Data::Dumper is messing things up, so let's remove those two lines and let's just check wether perl thinks the keys are actually references. ref() is a nice function that tells us what data type is behind a reference.

my %testHash = ( \@multiKey1, \@stuff1, \@multiKey2, \@stuff2, \@mult +iKey3, \@stuff3); @testHashKeys = keys %testHash; @testHashValues = values %testHash; $tmp = @testHashKeys; foreach (@testHashKeys){ print ref($_), ": "; print "deref bla = @$_ = $_\n"; } foreach (@testHashValues){ print ref($_), ": "; print "deref bla = @$_ = $_\n"; } __END__ Output: : deref bla = = ARRAY(0x98a9b4) : deref bla = = ARRAY(0x98a914) : deref bla = = ARRAY(0x98a844) ARRAY: deref bla = 6 7 = ARRAY(0x98aa04) ARRAY: deref bla = 4 5 = ARRAY(0x98a964) ARRAY: deref bla = 1 2 3 = ARRAY(0x3e8d8c)

So what it looks like, to me, is that hash keys can't hold references. I could be wrong, of course, but quite clearly in this case the references you specified as keys were turned into just strings. I'm curious to what others have to say, though - I can't quite believe one can't use a reference as a key, given that refs are just scalars anyway.


In reply to Re: reference as hash keys and values by muba
in thread reference as hash keys and values by ChangeManagement

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.