Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: calculating array average in HoA

by choroba (Cardinal)
on Apr 21, 2012 at 20:38 UTC ( [id://966409]=note: print w/replies, xml ) Need Help??


in reply to calculating array average in HoA

You are almost there. Just do not loop over values and keys:
use warnings; use strict; my %HoA = ( A => [2.34, 4.12, 5.32], B => [1.42, 8.15], C => [8.42, 0.23, 0.45, 6.43], ); foreach my $key (keys %HoA) { print $key, "\t", average($HoA{$key}), "\n"; } sub average { my $array_ref = shift; my $sum; foreach (@$array_ref) { $sum += $_; } my $av = $sum / @$array_ref; return $av; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://966409]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-19 20:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found