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

Subarray in a hash: Can't use string ("STRING") as a HASH ref while "strict refs" in use

by GeoJunkie (Initiate)
on Apr 08, 2014 at 13:35 UTC ( #1081503=perlquestion: print w/ replies, xml ) Need Help??
GeoJunkie has asked for the wisdom of the Perl Monks concerning the following question:

I've got a situation where I need to build an array of hashes of arrays of hashes (complex, yes, but it really is needed!) I'm getting a weird error, though. Here's a sampling of code that throws the error:
my @array_of_hashes; for (my $i=1; $i<=10; $i++) { $array_of_hashes[$i] = ( stuff => 'stuffy', goes => 'gooey', here => "more $i" ); $array_of_hashes[$i]{sub_data}[6] = "seven"; }

When I execute this, I get: "Can't use string ("more 1") as a HASH ref while "strict refs" in use" at the last assignment operation ($array_of_hashes$i{sub_data}6 = "seven";). Any ideas why and how I can go about adding a hash with a subarray in this way?

Thanks!

Comment on Subarray in a hash: Can't use string ("STRING") as a HASH ref while "strict refs" in use
Download Code
Re: Subarray in a hash: Can't use string ("STRING") as a HASH ref while "strict refs" in use
by choroba (Abbot) on Apr 08, 2014 at 13:39 UTC
    Round parentheses ( ... ) don't create a reference. Instead, use { ... } to create a hash reference:
    my @array_of_hashes; for (my $i=1; $i<=10; $i++) { $array_of_hashes[$i] = { stuff => 'stuffy', goes => 'gooey', here => "more $i" }; $array_of_hashes[$i]{sub_data}[6] = "seven"; }

    Update: In your original code, assignment to a hash value introduced a scalar context. In scalar context, the list in parentheses returned its last member, "more $i". You later tried to use this value as a hash reference.

    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      That did it! Is this just because it's a hash within an array? Normally, when defining a hash (as %hashname), you use round parentheses - at least that's what the Perl Cookbook says.
      Thanks!
        ( ) are used for assignments to arrays and hashes. [ ] are used for array references, { } are used for hash references. You are not assigning a hash, you are assigning a hash reference here.

        Updated.

        لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

        Compare

        %hash = (key1 => 'val1', key2 => 'val2'); $hash = {key1 => 'val1', key2 => 'val2'};
        The first one defines hash, the second defines reference to hash, which is scalar.

Re: Subarray in a hash: Can't use string ("STRING") as a HASH ref while "strict refs" in use
by AnomalousMonk (Monsignor) on Apr 08, 2014 at 17:58 UTC

    Had you been using warnings, Perl would have given you some hints that might have been enlightening:

    c:\@Work\Perl\monks>perl -wMstrict -le "my @array_of_hashes; for (my $i=1; $i<=10; $i++) { $array_of_hashes[$i] = ( stuff => 'stuffy', goes => 'gooey', here => \"more $i\" ); $array_of_hashes[$i]{sub_data}[6] = \"seven\"; } " Useless use of a constant (stuff) in void context at -e line 1. Useless use of a constant (stuffy) in void context at -e line 1. Useless use of a constant (goes) in void context at -e line 1. Useless use of a constant (gooey) in void context at -e line 1. Useless use of a constant (here) in void context at -e line 1. Can't use string ("more 1") as a HASH ref while "strict refs" in use a +t -e line 1.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (19)
As of 2014-07-25 17:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (174 votes), past polls