in reply to Re^2: array of hash
in thread array of hash

Can't use string ("CD") as a HASH ref while "strict refs" in use

Yes, that's the point you perhaps didn't fully understand. $cur{my}[2] is "CD", you can't dereference it as a hash under strict. Without strict, it's understood as symbol reference and the hash %CD is used, as I demonstrated in my original reply.

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

Replies are listed 'Best First'.
Re^4: array of hash
by carolw (Sexton) on May 27, 2014 at 11:18 UTC

    if I add your line to my program, I get the following error

    print Dumper $curr{my}[2], $curr{my}[2]{AB}, \%CD;

    Global symbol "%CD" requires explicit package name at ./test.pl line 14.

    In addition, how do you print the elements of $curr when using use strict?

    foreach (keys %{$curr{"my"}->[2]}){ print $_, " ", $curr{"my"}->[2]{$_}, " "; } print "\n";

      First, you have to
      use Data::Dumper;

      to be able to use Dumper.

      Second, %CD is created only without strict.

      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re^4: array of hash
by carolw (Sexton) on May 27, 2014 at 11:45 UTC

    it's seems that I can access $curr{"my"}->2{"AB"} correctly but not for CD as the key when using strict. Why?

    Do you have the solution how to build and access the array of hash correclty? I get more and more confused

      It's simple.
      $curr{my}[2] = 'CD';

      There's no hash under 2, only a string. So you can't access a hash. The solution is to build the structure correctly.

      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re^4: array of hash
by carolw (Sexton) on May 27, 2014 at 12:37 UTC

    I don't want to build. I already build %currHash and then, want to assign the whole %currHash to the 3rd array element of %curr. Then, I will change the value of the keys of the hash which is the 3rd element of array of %curr but I don't want that %currHash gets changed

    If I do such assignment, how come that the value of the CD doesn't exist when assigning %currHash or %tmp?

      You have to change the way you build the structure, because it's the source of the problem. Please read all the comments and linked pages again to understand.

      push @currArr, %currHash;

      This line does not insert a hash into an array. It flattens the hash to a list to be inserted. Use Data::Dumper to visualize the resulting structures. What structure do you exactly want to build? If you want to create a copy of a shallow hash, use

      push @currArr, { %currHash };

      To create a deep copy, see Clone or Storable.

      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re^4: array of hash
by carolw (Sexton) on May 27, 2014 at 12:31 UTC

    Also how to access the value of CD using strict?

Re^4: array of hash
by carolw (Sexton) on May 27, 2014 at 12:51 UTC

    I have no idea. push @currArr,%currHash doesn't seem to be correct and can't think of any thing else. I just want to build an array of hash; that is, the 3rd el of @currArr is %currHash