in reply to Mean and standard deviation loop

SixShot:

When your input data is sorted, I normally approach it like this:

# Compute average value for each group my ($prev_key, $cnt, $sum); foreach my $line (@data) { my ($key, $val) = parse_line($line); if (defined($prev_key) and $prev_key eq $key) { # Same group as last time, accumulate results ++$cnt, $sum += $val; } else { # New group starting. Print results from # previous group, then start new one. print "Group $prev_key avg=", $sum/$cnt, ", cnt=", $cnt, "\n"; $prev_key = $key, $cnt=1, $sum=$val; } } # Print results from last group. print "Group $prev_key avg=", $sum/$cnt, ", cnt=", $cnt, "\n";

And when you're data isn't sorted, you can always sort it first!

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Replies are listed 'Best First'.
Re^2: Mean and standard deviation loop
by SixShot (Novice) on Jun 17, 2012 at 13:42 UTC

    Hi roboticus, Thanks for your help, can l ask what the parse_line subroutine is?

      SixShot:

      Sure, it's just a routine to pull out the value(s) you're interested in. In your case, you could probably use split and adjust the variable names on the left to represent the value(s) you want. You can use 'undef' for the columns holding values you don't care about, or just give them names. I frequently do the latter for documentation (so I don't have to look up the file format to figure out what a particular column is in case I want to modify the program).

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.