Your main problem is here:

foreach my $key (keys %HoA) { foreach my $value (values %HoA) { my @Array = $value;

For each key in your hash, you're looping through all the values in your hash. You want to loop through the keys, and for each one, average the values in the particular array that it points to. Also, in the last line above, assigning a scalar to an array will simply make the scalar the first element of the array. Since your scalar is an array reference, you want to dereference it to get the array it points to. Which gives you these lines instead of the above:

for my $key (keys %HoA){ # loop through the keys my $value = $HoA{$key}; # get the array ref for this key my @Array = @{$value}; # dereference it to get the array

You can shorten that up and eliminate a step or two once you understand it, but that's the process broken down into small steps.

One last thing: no need to instantiate $average outside your loop, since you only use it inside the loop. Better to instantiate it inside, in the narrowest possible context.

Edited: Had a typo, $Array instead of @Array.

Aaron B.
My Woefully Neglected Blog, where I occasionally mention Perl.


In reply to Re: calculating array average in HoA by aaron_baugher
in thread calculating array average in HoA by benben

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.