#!perl use strict; use warnings; use autodie; use Data::Dump qw(dump); my %data; #open my $in,'<', "testscores.txt"; while (<DATA>){ my ($name, @score) = split /[\s=,]+/; $data{$name} = \@score; } dump \%data; my $avg = get_avg(\%data); dump $avg; sub get_avg { my ($hash_ref) = @_; my %avg = (); for my $name (sort keys %$hash_ref){ my $sum = 0; my $count = 0; for my $score (@{$hash_ref->{$name}}){ $sum += $score; ++$count; } $avg{$name} = $sum/$count; } return \%avg; } __DATA__ tim = 40 , 70, 50, 80 john = 98, 97 , 100, 89 eden = 87, 56, 89, 97 pepe = 93 ,91, 94, 90 leah = 100, 99, 99, 100 tony = 89, 94, 100, 89 matt = 68, 70, 75, 73
poj

In reply to Re: passing multi dimensional hashes into a sub by poj
in thread passing multi dimensional hashes into a sub by perlynewby

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.