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

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.

لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
  • Comment on Re: Subarray in a hash: Can't use string ("STRING") as a HASH ref while "strict refs" in use
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: 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:48 UTC
    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.

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

        No, ( ) is used for lists. Lists are converted into arrays by assignment. Also, lists are converted into hashes by assignment.

        Example: note the addition of the `undef` to make the hash work properly.

        #!/usr/bin/env perl use strict; use warnings; use feature qw( :all ); # -------------------------------------- # Modules use Data::Dumper; # Make Data::Dumper pretty $Data::Dumper::Sortkeys = 1; $Data::Dumper::Indent = 1; # Set maximum depth for Data::Dumper, zero means unlimited local $Data::Dumper::Maxdepth = 0; # -------------------------------------- my @a = my %h = ( 1, 2, 3 ); say Dumper \@a; say Dumper \%h;

      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.