in reply to can't use string as hash ref

I've run into the same error message numerous times while trying to setup hashes for my various gui programs. Look at this simple example, and see that only line 13 causes an error.

It is sort of confusing to me as to why, but if you have a multi-level hash, the first key must be able to vary, it cannot be a scalar reference to some other data structure or object.

#!/usr/bin/perl use warnings; use strict; my %hash; my @array = (1..100); # this will be ok $hash{\@array} = 42; # but this will issue an error, because \@array # cannot be used as a key which varies $hash{\@array}{1} = 42; # this will be ok $hash{1}{\@array}{1} = 42; #this will be ok $hash{1}{\@array}{1}{1} = 42;

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh

Replies are listed 'Best First'.
Re^2: can't use string as hash ref
by morgon (Priest) on Oct 26, 2010 at 02:56 UTC
    I think your explanations are very misleading ...

    my %hash; my @array = (1..100); # this will be ok $hash{\@array} = 42;
    What happens here is that \@array gets stringified, so the key in the hash is a string (not a reference!) that looks something like "ARRAY(0x987f468)".

    Such a construction is (unfortunately) not causing an error, but it almost never is what you want, so it is not ok. It usually is a programming error.

    my %hash; my @array = (1..100); # this will be ok $hash{\@array} = 42; # but this will issue an error, because \@array # cannot be used as a key which varies $hash{\@array}{1} = 42;
    What happens here is that you stringify \@array twice (resulting in the same string each time), so in $hash{\@array}{1} you try to use $hash{\@array} as a hash, but you have assigned 42 (which is not a hash) to that before. The error has nothing to do at all with \@array being used as a hash-key.

    Witness:

    use strict; my %hash; my @array = (1..100); $hash{\@array} = {}; $hash{\@array}{1} = 42;
    This does not produce an error (but I would not say it is ok just because of that).

    If you want to use references as hash-keys you have to use Tie::RefHash.

      Good point is made. Just wanted to summarize it. Every time when you see "Can't use something as reference to something" error, search for the existing element of the hash or array which is not reference as you expect it to be.

      Non-existing elements are automatically created by perl for you, and probably this is what is confusing you.

Re^2: can't use string as hash ref
by Anonymous Monk on Oct 25, 2010 at 12:30 UTC
    # this will be ok $hash{1}{\@array}{1} = 42; #this will be ok $hash{1}{\@array}{1}{1} = 42;
    The latter doesn't work either (it just doesn't issue an error here, 'cos the script is dying after the first problem on line 13).

    The reason is simply that if you assign 42 to $hash{\@array}, or $hash{1}{\@array}{1} for that matter, and then try to deference that value in the next statement, you're essentially trying to say

    "42"->{1} = 42
    which obviously doesn't make sense.

      Thanks for all of the replies. It sounds like I have a dereferencing issue. I'm going to take another whack at it and post back. Each time I use hashes and think I've got it 'licked' ....