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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.