I suspect you may be trying to make things more difficult than they need to be. References can be tricky, but once you get the hang of them they make so many other things MUCH easier.
I tried to follow the logic of your example, but I'm afraid I didn't understand the rationale for doing things they way you did. For example, you are manipulating an array inside a hash of hashes (HoH), but you never make use of either level of hash. I assume the example may be a simplified version of the real problem, so I retained that structure in my example below. If there isn't any reason to keep the HoH structure, then simply using the array would be easier.
I rewrote your example (below). You'll notice that I added a few things. First and foremost, strict and warnings are your friends. Use them, especially when you're trying to learn new syntax. There are many problems with your example code that would have been brought to your attention had you used them. Second, a module like Data::Dumper comes in handy when trying to understand complex data structures.
use strict; use warnings; use Data::Dumper; my @fruit = qw( Apple pear Bananna Peach ); my $new_fruit = "Grapes"; my %fruits; $fruits{0}{fruit} = \@fruit; print "Original fruits:\n", Dumper( \%fruits ); push( @{ $fruits{0}{fruit} }, $new_fruit ); print "New fruits:\n", Dumper( \%fruits ); print "There are ", scalar @{ $fruits{0}{fruit} }, " pieces of fruit:\ +n"; foreach my $f ( @{ $fruits{0}{fruit} } ) { print "$f\n"; }
I left the code uncommented intentionally. Try to follow what is going on, but if you need help feel free to ask.
There are lots of good resources to learn about references and visualizing data structures. If I know planetscape, she will post an extensive list. :-)
HTH
Update: In case planetscape is off hunting knights, start with perlref, perlreftut, perldsc, perllol, PerlMonks tutorials about references, and How can I visualize my complex data structure?.
In reply to Re: Array in a Hash
by bobf
in thread Array in a Hash
by g_speran
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |