Link to home
Start Free TrialLog in
Avatar of Europa MacDonald
Europa MacDonaldFlag for United Kingdom of Great Britain and Northern Ireland

asked on

counting average values

the code below sorts numbers into various groupings

the average value; is it possible the

1. get the highest average value
2. the lowest average value
3. the average, average value ?
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use List::Util qw(sum);
 
##### Open files
open(my $IN,"invim2.vim") or die "Could not open input: $!\n";
open(my $OUT,">outvim2.vim") or die "Could not open output: $!\n";
 
##### Step 1: read a line, and split it on comma
while(<$IN>){
        chomp;
        my @arr = split /,/;
        next unless @arr;
 
        # discard 1st and last item
        shift @arr; pop @arr; pop @arr;
 
        ##### Step 2: Sort data into ascending order
        @arr = sort {$a <=> $b} @arr;
        print "arr @arr\n";
 
        ##### Step 3: Calculate difference between neighboring pairs
        my @diff = ($arr[0], map {$arr[$_] - $arr[$_-1]} (1..$#arr));
        print "diff @diff\n";
 
        ##### Step 4: Calculate amount in each group
        my @groups = (0)x5;
        foreach my $a (@arr) {
                $groups[$a/10]++;
        }
        print "group @groups\n";
        ##### Step 6: Calculate average
 
        my $avg1 = sum(@arr)/@arr;
        printf "arr avg %.2f\n", $avg1;
        my $avg2 = sum(@diff)/@diff;
        printf "diff avg %.2f\n", $avg2;
 
        ##### Step 5: Print results
        print $OUT join("\t", @arr) . "\tavg " . sprintf("%.2f\t", $avg1) . "group " . join("", @groups) . "\n";
        print $OUT join("\t", @diff) . "\tavg " . sprintf("%.2f\t", $avg2) . "\n";
        print $OUT "\n";
}
 
 
close($IN);
close($OUT);

Open in new window

Avatar of mrjoltcola
mrjoltcola
Flag of United States of America image

Averages from which dataset, the input dataset or the diff dataset?
Also how to format the output?
ASKER CERTIFIED SOLUTION
Avatar of mrjoltcola
mrjoltcola
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Europa MacDonald

ASKER

sorry mrjoltcola, net crashed last night and I went to bed :-)

what I was sking for:
the code just now will calculate the average value and the average difference value.

I want to know, for both averages

the highest value average
the lowest value average
the average of the range of value averages (the mean average)

I will try that code above and let you know

thankyou

great