Perl 脚本中的统计
我有以下问题: 我想创建一个 perl 脚本,从文本文件(包含几列数字的文件)读取并计算一些统计数据(平均值、中位数、标准差、方差)。我已经构建了一个脚本,但是由于我还没有爱上 perl,所以我无法解决它的语法问题...... 这是我的 Perl 脚本..
#!/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 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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码中的错误:
需要引用 data.txt。您没有检查打开的返回值,例如
... 或 die $!
。您应该使用词法文件句柄和三个参数open,例如open my $fh、'<'、"data.txt"或die $!
。除了从参数列表中删除第一个值(然后立即将其丢弃)之外,这不会执行任何操作。
您使用
\t
作为裸字,它应该是正则表达式,/\t/
。您的@array
是在 while 循环的词法范围内声明的,并且在此块之外将是未定义的。这两个变量都没有声明,这将导致脚本在使用 strict 时死掉(这是一个非常好的主意)。使用
my $N
来解决这个问题。另外,$N
也不是一个很好的变量名。这将无济于事。
$x
未声明(见上文),也未定义。整个语句什么也不做,就像有一行3;
。我相信您会得到一个错误,例如在 void context 中无用地使用变量
。这将
$N
增加到 1,这是一件无用的事情,因为您只用上面的几行将其初始化为 0。嗯......这样的例子还在继续。我建议你从小处开始,使用严格和警告,因为它们是非常好的工具,并一一解决错误。一个非常好的主意是为您的计算创建子例程,例如:
转到 perldoc.perl.org 并阅读文档。特别有用的是与语法相关的和 perlfunc。
另外,您应该知道此功能可以在模块中找到,您可以在 CPAN 中找到这些模块。
Errors in your code:
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 $!
.This does nothing except remove the first value from you argument list, which is then promptly discarded.
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.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.This will do nothing.
$x
is not declared (see above), and also undefined. The whole statement does nothing, it is like having a line3;
. I believe you will get an error such asUseless use of variable in void context
.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.:
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.
您的主要问题是您尚未声明变量,例如
$N
、$max
等。您需要使用
my
引入所有新变量当你第一次引用它们时。就像您对$array
和$i
所做的那样。例如应该变成
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 exampleShould become