Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
laziness, impatience, and hubris

building array of hashes

by fionbarr (Monk)
on Jun 28, 2013 at 18:48 UTC ( #1041316=perlquestion: print w/ replies, xml ) Need Help??
fionbarr has asked for the wisdom of the Perl Monks concerning the following question:

my @label_array = qw (Title Section Subsection Class Category Degree +Attempt CountOfCharges); for ( 0 .. ( $offense_line_count - 1 ) ) { my $value = $offense_value_array[$_]; # next if ( !defined $value ); my $label = $offense_label_array[$_]; $hash{$label} = $value; # print "$label -> $value\n"; # build an array of hashes...if the 'CountOfCharges' is greater th +an 1 # push another copy of the hash on the array if ( $label eq 'CountOfCharges') { $num_chgs = int($value); if ($num_chgs > 1 ) { print "ref = $referencenumber....($num_chgs) \n" } for (0 .. ($num_chgs - 1)) { print "\n\n$_ pushing $hash{Section} / $hash{Subsection} +\n"; push @AOH, \%hash; } } }
I am pushing unique hashes onto an array but each array element is IDENTICAL when I examine it. What stupid mistake am I making? I look at the array like 'x $AOH[0]->{Section}' and each element of the array is identical

Comment on building array of hashes
Download Code
Re: building array of hashes
by fionbarr (Monk) on Jun 28, 2013 at 18:55 UTC
    that is $AOH1->{Section} is identical to $AOH[0]->{Section} etc.
Re: building array of hashes
by SuicideJunkie (Priest) on Jun 28, 2013 at 19:33 UTC

    FYI, you can edit your original post to clarify with an Edit: blah blah

    Whenever you have multiple elements that are mysteriously identical, you've almost certainly got two references to the same thing in your set.

    $AoH[0] >---\ \ -----> $hashref / $AoH[1] >---/

    %hash is declared somewhere outside of the code you posted. Inside your loop, you push a reference to that hash onto your array. You then push more references to the SAME hash until the loop ends.

    If you want different, independent hashes in each array element, then push @AoH, {Section=>$sec, Subsection=>$subsec, foo=>$bar}; to get references to a new anonymous hash each time.

    Alternatively, you can do a deep copy on a template hash before tweaking it and putting a ref to the new copy into your array

Re: building array of hashes
by Eily (Hermit) on Jun 28, 2013 at 19:35 UTC

    Since you do not declare your hash inside of your for loop, it is always the same one you push into the array. You do not have a new one with new values, you just replace them.

    There are two things you can do. Either declare your hash lexically (with my) inside the for loop, and you'll have a brand new one each time, or youcan make a new one to push into AOH. With push @AOH, { %hash }; # copy of %hash here

    Both are possible, but it would probably be better to have your my %hash in the for loop, because you can be sure to have only new values, not ones that you somehow didn't delete or change.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1041316]
Approved by hdb
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2014-04-17 22:25 GMT
Find Nodes?
    Voting Booth?

    April first is:

    Results (458 votes), past polls