open(FH,"$ARGV[0]") or die;
my @temp=<FH>;
close FH;
my $mean = Mean(\@temp);
my $var = variance(\@temp);
print "$var\n";
sub estimate_variance {
my ($arrayref) = @;
my ($mean,$result) = (mean($arrayref),0);
foreach (@$arrayref) { $result += ($ - $mean)**2 }
return $result / $#{$arrayref};
}
error shown:
Undefined subroutine &main::variance called at variance_try1.pl line 9.
It might be easier if you used a standard proven statistics module of which there are a number on CPAN.
For example, I have modified your code to work with the CPAN Module Statistics/Descriptive
#!/usr/local/bin/perl -w
use strict;
use Statistics::Descriptive;
open(FH, "$ARGV[0]") or die "No file specified\n";
my @temp=<FH>;
close FH;
my $stat = Statistics::Descriptive::Full->new();
$stat->add_data(\@temp);
my $mean = $stat->mean();
my $variance = $stat->variance();
my $num = $stat->count();
print "Number of Values = $num\n",
"Mean = $mean\n",
"Variance = $variance\n";