Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: array of hash

by choroba (Cardinal)
on May 27, 2014 at 09:35 UTC ( [id://1087503]=note: print w/replies, xml ) Need Help??


in reply to array of hash

I read your post several times, but I still don't get it. What output do you expect?

Update: Now I see it! You don't use strict (refs).

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

outputs

$VAR1 = 'CD'; $VAR2 = 1; $VAR3 = { 'AB' => 1 };

See Can you use string as a HASH ref while "strict refs" in use? for an explanation.

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

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

    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();
      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";

        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

        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?

        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

Re^2: array of hash
by carolw (Sexton) on May 27, 2014 at 10:59 UTC

    Cont.

    by the way, despite use strict, the value of %currHash is modified. So the problem is still there.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1087503]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-19 20:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found