Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

can't use string as hash ref

by spickles (Scribe)
on Oct 25, 2010 at 03:03 UTC ( [id://867123]=perlquestion: print w/replies, xml ) Need Help??

spickles has asked for the wisdom of the Perl Monks concerning the following question:

I've taken a look at all of the postings for this error on Perl Monks, and I still didn't get an answer to my situation. The error is understandable. What I don't understand is what I can do to fix it. The hash I'm referencing is a query to a MySQL database. I am using fetchall_hashref to get the results and create the hash. The problem is that no matter what column I use as the 'key' to the hash, none of the values in the database are anything but strings. I thought it would be best to key the hash using the key of the MySQL table, but the key is 'id' and the values are incrementing numbers. Despite this, the error still reports those hash keys as strings. Does this have something to do with the fetchall_hashref? Should I use something other than fetchall_hashref, or is there a way to ensure that when the hash is built they are of type 'HASH' and not strings?

Regards,
Scott

Replies are listed 'Best First'.
Re: can't use string as hash ref
by ikegami (Patriarch) on Oct 25, 2010 at 03:39 UTC

    The hash I'm referencing is a query to a MySQL database.

    No, the error means you don't have a reference to a hash.

    Despite this, the error still reports those hash keys as strings.

    That's not what the error is reporting. For starters, hash keys are necessarily strings.

    is there a way to ensure that when the hash is built they are of type 'HASH' and not strings?

    The question makes no sense. If a hash is built, it's not a string.

Re: can't use string as hash ref
by kcott (Archbishop) on Oct 25, 2010 at 05:33 UTC

    Here's the documentation for the fetchall_hashref method.

    If that doesn't help, please post your code so we can help you further.

    -- Ken

Re: can't use string as hash ref
by aquarium (Curate) on Oct 25, 2010 at 04:39 UTC
    from memory what you get is a hashref to the hash holding the result of your query. hence first you need to dereference the initial hashref. so something like this during your fetch processing loop:
    my $column1value = \$result_hash{'col1'};
    note the back slash, dereferencing the hash reference. there are examples of fetch loop processing code using the fetchall_hashref on the web and in the documentation for DBI.
    the hardest line to type correctly is: stty erase ^H
      note the back slash, dereferencing the hash reference

      I don't understand what you're trying to say here, and I'm fairly certain this will only confuse other people. The backslash operator takes a reference. You might end up with a reference to a reference if the value in the hash associated with col1 is itself a reference.

      ... and that's not the OP's problem, anyway. The real problem is attempting to dereference something which is not a reference at all.

Re: can't use string as hash ref
by zentara (Archbishop) on Oct 25, 2010 at 11:18 UTC
    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
      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.

      # 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' ....

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://867123]
Approved by aquarium
Front-paged by aquarium
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-19 14:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found