Perl Script to find Standard Deviation and Mean, Median with example code
In statistics Mean, Median, Mode and Standard Deviation is common term which is mostly used in industry. The below perl program will help us to find the Standard deviation, mean( i.e average), median and mode. Mean is just to find the average of given numbers. You can know more about standard deviation using this Worksheet for Standard Deviation.
//source code standard deviation in perl
#!/usr/bin/perl
use strict;
use warnings;
my @data = <STDIN>;
chomp @data;
@data = sort { $a <=> $b } @data;
if (not @data) {
print "Please give some data to calculate standard deviation
";
exit;
}
my $total = 0;
foreach my $v (@data) {
$total += $v;
}
my $average = $total / @data;
my $median = @data % 2 ? $data[(@data-1)/2]
: ($data[@data/2-1]+$data[@data/2])/2
;
my $sqtotal = 0;
foreach my $v (@data) {
$sqtotal += ($average-$v) ** 2;
}
my $std = ($sqtotal / @data) ** 0.5;
print "Min: $data[0] Max: $data[-1] Total: $total count: "
. @data . " Mean: $average
";
print "Median: $median $sqtotal Standard deviation: $std
";
You can check the result using this online Standard Deviation Calculator
