Reputation: 31
I have the following question: I want to create a perl script that reads from a text file (file with several columns of numbers) and calculate some statistics (mean, median, sd, variance). I already built one script, but as I am not in love yet with perl, I can't fix the problems of syntax on it... Here is my perl script..
#!/usr/bin/perl -w
use strict;
open(FILEHANDLE, data.txt);
while (<FILEHANDLE>) {
shift @ARGV;
my @array = split(\t,$_);
}
close(FILEHANDLE);
###### mean, sum and size
$N = $sum = 0;
$array[$x-1];
$N++;
$sum += $array[$x-1];
###### 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) {
print "$median = $sorted[int($N/2)]\n"; ## check this out
};
else ($median = ($sorted[$N/2] + $sorted[($N/2)-1]) / 2)) {
print "$median\n"; # check this out
};
##### 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 "$mean\n";
print "$median\n";
print "$qt1\n";
print "$var\n";
print "$std\n";
exit(0);
I want to get it working. Please help. THANKS IN ADVANCE!
Upvotes: 0
Views: 1559
Reputation: 8588
Your main problem is you have not declared your variables such as $N
, $max
, etc.
You need to introduce all new variables with my
the first time you reference them. Just like you did with $array
and $i
. So for example
$N = $sum = 0;
Should become
my( $N, $sum ) = ( 0, 0 );
Upvotes: 0
Reputation: 67900
Errors in your code:
open(FILEHANDLE, data.txt);
data.txt needs to be quoted. You are not checking the return value of the open, e.g. ... or die $!
. You should use a lexical filehandle and three argument open, e.g. open my $fh, '<', "data.txt" or die $!
.
shift @ARGV;
This does nothing except remove the first value from you argument list, which is then promptly discarded.
my @array = split(\t,$_);
You are using \t
as a bareword, it should be a regex, /\t/
. Your @array
is declared inside a lexical scope of the while loop, and will be undefined outside this block.
$N = $sum = 0;
Both variables are not declared, which will cause the script to die when you use strict (which is a very good idea). Use my $N
to solve that. Also, $N
is not a very good variable name.
$array[$x-1];
This will do nothing. $x
is not declared (see above), and also undefined. The whole statement does nothing, it is like having a line 3;
. I believe you will get an error such as Useless use of variable in void context
.
$N++;
This increments $N
to 1, which is a useless thing to do, since you only a few lines above initialized it to 0.
Well.. the list goes on. I suggest you start smaller, use strict and warnings since they are very good tools, and work out the errors one by one. A very good idea would be to make subroutines of your calculations, e.g.:
sub sum {
# code here
return $sum;
}
Go to perldoc.perl.org and read the documentation. Especially useful would be the syntax related ones and perlfunc.
Also, you should be aware that this functionality can be found in modules, which you can find at CPAN.
Upvotes: 4