Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: HOW to calculate the column data

by ikegami (Patriarch)
on Dec 03, 2009 at 21:04 UTC ( [id://810946]=note: print w/replies, xml ) Need Help??


in reply to HOW to calculate the column data

Group them using a hash
my %vals_by_type; while (<DATA>) { chomp; my ($type, $val) = split ' '; push @{ $vals_by_type{$type} }, $val; }

Then average each type individually

use List::Util qw( sum ); for my $type (sort keys %vals_by_type) { my $vals = $vals_by_type{$type}; my $avg = sum( map $_/@$vals, @$vals ); printf("%s %.2f\n", $type, $avg); }

Replies are listed 'Best First'.
Re^2: HOW to calculate the column data
by jwkrahn (Abbot) on Dec 03, 2009 at 21:18 UTC

    No need to use an array, you just need to store two values, the total and the count:

    my %vals_by_type; while ( <DATA> ) { my ( $type, $val ) = split; $vals_by_type{ $type }{ total } += $val; $vals_by_type{ $type }{ count }++; } for my $type ( sort keys %vals_by_type ) { my $avg = $vals_by_type{ $type }{ total } / $vals_by_type{ $type } +{ count }; printf "%s %.2f\n", $type, $avg; }

      The difference is that I divided each value *before* summing them for extra precision. But I'll grant you that it's surely not needed here.

      Storing them in an array is also useful if you want to perform more than one operation, especially if the operation requires all the elements (like finding the median).

      By the way,
      $vals_by_type{ $type }{ count }++;
      is less efficient than
      ++$vals_by_type{ $type }{ count };

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-04-20 00:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found