Calculating average using a perl script


Ok here is the basic perl script for calculating average.

I assume you know how to run the script if you dont ( I will put up a tutorial later so for now dig around in other forms )

Perl Script for calculating average from another file

#!/usr/bin/perl
#calculate averate from a file

use strict;

my $total = 0;
my $count =0;
while (my $line = <>) {
      $total +=$line;
      $count ++=;
}
print "Average = ", $total / $count, "\n";

So the  explaniation for the above code is…

use strict; – allow to find simple mistakes

to find the $average need to calculate

my $total

and the total number of lines

my $count

At the begining of the loop these values are 0.

therefore define the inital value

my $total  = 0;
my $count =0;

So now what you need to do this read each line andadd each value consequtively.

while (my $line = <>) {
   $total += $line;
   $count ++=;
 }
print "Average = ", $total / $count, "\n";

Read the first line.  Then add then and the value read to the total inital value  of 0.

   $total = $line + $total

calculate the total

   $count = $count + 1

calculate the total number of lines read
this can be written in short form as below

   $total += $line;
   $count ++=;

then go back in loop adding value conseutively until there are no more lines to read and then print the line

print "Average = ", $total / $count, "\n";

Hope this is clear I will put a anotated diagram up with more explanation later on.

Advertisements

3 thoughts on “Calculating average using a perl script

  1. Very good!, I was looking for something like this to do some calculations, like here the average (mean), median, sd, range (max – min values) and quartiles (1st and 3ed) from a file.
    I hope you can help me.

    Thanks.

    • Thank you for the comment.

      You just gave me the idea for a blog post 🙂

      What exactly do you need? Have you got any perl scripts that you tried? I’m quite new to perl so bare with me on this.

      • hello Shavin,
        Yeah I’ve got one Perl Script with the commands to calculate some Statistics.
        but, it’s a mess and, sine I’m new in Perl, I cant get it working.
        here is it, It may have some redundancies.
        }
        close(FILEHANDLE);

        ####################################################
        # mean, sum and size #
        ####################################################

        my ( $N, $sum ) = ( 0, 0 );
        # create a loop, and generate a counter (still dont
        # which loop I should use
        $array[$x-1];
        $N++;
        $sum += $array[$x-1];
        # outside the loop to calculate the mean
        $mean = $sum/$N;
        print “$mean\n”;

        ####################################################
        # minimum and the maximum #
        ####################################################

        ($min = 0, $max = 0);
        $max = $array[$x-1]
        if ($max < $array[$x-1]), (my@sorted = sort { $a $b } @samples)
        {
        print join(” “,@sorted);
        };

        ####################################################
        # median #
        ####################################################

        if ($N % 2==1) {
        $median = $sorted[int($N/2)];
        }
        print $sorted[$N/2];

        else {
        $median = ($sorted[$N/2] + $sorte [($N/2)-1]);
        $med = $median/2;
        print “$med\n”;
        };

        #####################################################
        # quantiles 1º and 3º #
        #####################################################

        if $qt1 = $sorted[$r25-1] {
        print “\n”; # check this out
        };
        else $qt1 = $fr*($sorted[$ir] – $sorted[$ir-1]) + $sorted[$ir-1] {
        print “\n”; # check this out
        };

        #####################################################
        # variance #
        #####################################################

        for (my $i=0;
        $i<scalar(@samples);
        $i++)
        {
        $Var += ($samples[$i]-$mean)**2;
        $Var = $Var/($N-1);
        };

        #####################################################
        # standard error #
        #####################################################

        ($Std = sqrt($Var)/ sqrt($N));

        #####################################################

        print "$min\n";
        print "$max\n";

        print "$median\n";
        print "$qt1\n";
        print "$var\n";
        print "$std\n";

        exit(0);

        Thanks, for replay.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s