in reply to How can I push a hash onto an array?
This is a job for references. See perlman:perlref for the whole story. The abridged version is that perl will let you pass around "pointers" to other structures. These are references. Basically they let you have a variable, a scalar specifically, which points to some other 'thing' (read very technical term :). That way you can do things just like you want to - build complex data structures (not to mention objects, anonymous data structures, and other wonderful beasties).
The way it works is you tell perl that some new scalar is now assigned the 'location' of another data structure. The example is a bit clearer than the language:
use strict; # or DIE : ) use warnings; # or SUFFER : ) my %hash = ( one => 'this', two => 'that' ); my $ref = \%hash; # the \ tells perl to store a reference # then you can use the reference like you would the hash by DEreferenc +ing it print "$ref->{one}"; # prints 'this'
The \ in front of %hash is what clues perl in that $reference should be a reference to that hash. The -> is the de-referencer. Read it like get $ref and when you see that it is a reference to something else follow (arrow, get it? :) that reference to the 'thing' referred to, where, since you used the {} (read hashy things), you find a hash and get that key's value. So your code becomes:
#!/usr/bin/perl -w # -w does same as use warnings above, but for older + perls use strict; my (%item, @kung); $item{foo} = "5"; $item{bar} = "7"; push @kung, \%item; # note the slash print "Kungfoo is ->", $kung[0]{foo}, "<-\n";
Note that the way you referred (ALL puns intended) to your value was just fine. You could also have used $kung[0]->{foo}. Perl gives you the convenience of leaving out the arrows sometimes. Did I say see perlman:perlref for the details yet?
Originally posted as a Categorized Answer.