Solved

counting average values

Posted on 2009-07-11
5
211 Views
Last Modified: 2012-05-07
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

0
Comment
Question by:MichaelGlancy
  • 3
  • 2
5 Comments
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24832793
Averages from which dataset, the input dataset or the diff dataset?
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24832797
Also how to format the output?
0
 
LVL 40

Accepted Solution

by:
mrjoltcola earned 500 total points
ID: 24832833

#!/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";
 

my @avgs1;

my @avgs2;

my ($maxavg1, $minavg1, $maxavg2, $minavg2);
 

##### 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;
 

        ##### 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;

        if(!$maxavg1 || $avg1 > $maxavg1) { $maxavg1 = $avg1; }

        if(!$minavg1 || $avg1 < $minavg1) { $minavg1 = $avg1; }

        push @avgs1, $avg1;
 

        my $avg2 = sum(@diff)/@diff;

        printf "diff avg %.2f\n", $avg2;

        if(!$maxavg2 || $avg2 > $maxavg2) { $maxavg2 = $avg2; }

        if(!$minavg2 || $avg2 < $minavg2) { $minavg2 = $avg2; }

        push @avgs2, $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";

}
 

printf "input: maxavg %d minavg %d avgavg %d\n", $maxavg1, $minavg1, sum(@avgs1)/@avgs1;

printf $OUT "maxavg %d minavg %d avgavg %d\n", $maxavg1, $minavg1, sum(@avgs1)/@avgs1;

printf "diff: maxavg %d minavg %d avgavg %d\n", $maxavg2, $minavg2, sum(@avgs2)/@avgs2;

printf $OUT "maxavg %d minavg %d avgavg %d\n", $maxavg2, $minavg2, sum(@avgs2)/@avgs2;
 

close($IN);

close($OUT);

Open in new window

0
 

Author Comment

by:MichaelGlancy
ID: 24834287
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

0
 

Author Closing Comment

by:MichaelGlancy
ID: 31602498
great
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now