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

perhaps I didn't fully understand your comment but i changed my program based on use strict. I still have problem

If I don't use the hash ref, I get this error Can't use string ("CD") as a HASH ref while "strict refs" in use at ./test.pl line 15.

and if i use hash ref, I don't get any value for the key CD Don't know why?! curr CD AB 1 currhash CD AB 1

The desired output is curr CD 0 AB 1 currhash CD 0 AB 0

#!/usr/bin/perl use strict; sub parseFiles{ my @currArr=(); my %curr; my %tmp = $_[0]; push @currArr,'+1'; push @currArr,'u'; push @currArr,%tmp; # push @currArr,%currHash; in the first case $curr{"my"}=[@currArr]; $curr{"my"}->[2]{"AB"}++; #print scalar(keys %{$curr{"my"}->[2]}), "\n"; #print $curr{"my"}->[0], " ", $curr{"my"}->[1], "\n"; print "curr\n"; foreach (keys %{$curr{"my"}->[2]}){ print $_, " ", $curr{"my"}->[2]{$_}, " "; } print "\n"; print "currhash\n"; foreach (keys %tmp){ print $_, " ", $tmp{$_}, " "; } print "\n"; } sub storeHash{ my %currHash; $currHash{"AB"}=$currHash{"CD"}= 0; return %currHash; } sub main{ my %currHash; %currHash=storeHash(); parseFiles(%currHash); } main();

Replies are listed 'Best First'.
Re^3: array of hash
by choroba (Cardinal) on May 27, 2014 at 11:00 UTC
    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.

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

      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.

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

      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.

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

      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.

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

      Also how to access the value of CD using strict?

      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