in reply to reference as hash keys and values

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.

Replies are listed 'Best First'.
Re^2: reference as hash keys and values
by davido (Cardinal) on Jul 07, 2011 at 02:02 UTC

    Good process of testing and deduction. Deductive reasoning like that is one of those qualities present in programmers who are capable of solving problems on their own. I believe logical and deductive reasoning is an attribute that must accompany success as a programmer. Your testing can be summarized well by perlref:

    WARNING

    You may not (usefully) use a reference as the key to a hash. It will be converted into a string:

    $x{ \$a } = $a;

    If you try to dereference the key, it won't do a hard dereference, and you won't accomplish what you're attempting. You might want to do something more like

    $r = \@a; $x{ $r } = $r;

    And then at least you can use the values(), which will be real refs, instead of the keys(), which won't.

    The standard Tie::RefHash module provides a convenient workaround to this.

    References are passed around as scalars, but keys are indices composed of string values, not full scalar citizens.


    Dave

Re^2: reference as hash keys and values
by roboticus (Chancellor) on Jul 07, 2011 at 01:53 UTC

    muba:

    Nice post: ++. Experimentation is a good way to test your theories. Of course, digging around the documentation can also be useful. Glancing through perldoc perldata yields:

    Perl has three built-in data types: scalars, arrays of scalars, and
    associative arrays of scalars, known as "hashes". A scalar is a single
    string (of any size, limited only by the available memory), number, or
    a reference to something (which will be discussed in perlref). Normal
    arrays are ordered lists of scalars indexed by number, starting with 0.
    Hashes are unordered collections of scalar values indexed by their
    associated string key.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.