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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |