我可以转换这个 Perl 脚本以使其从文件中读取吗?
我想使用 Perl 脚本获取特定数据列的中位数,我有一个脚本可以读取脚本内数组中的值。
my (@vals, $med);
@vals =(12, 23, 34, 21, 66,66, 34, 87);
print "UNSORTED: @vals\n"; #sort data points
@vals = sort(@vals);
print "SORTED: @vals\n"; #test to see if there are an even number of data points
if( @vals % 2 == 0) { #if pair then:
$sum = $vals[(@vals/2)-1] + $vals[(@vals/2)];
$med = $sum/2;
print "The median value is $med\n";
}
else { #if odd then:
print "The median value is $vals[@vals/2]\n";
}
exit;
我可以以某种方式对其进行转换,使其从多列的文件中读取数据并计算所选列的中位数吗?就像在 shell 命令上输入 ./median.pl 1 column_numbers.tbl 一样。 我尝试过此操作,但文件 data.txt 只有一列
my (@vals, $med, $sum1, @numbers, @sorted);
open (COLUMN, "< data.txt") || die "Can not open file : $! ";
my @not_sorted = <COLUMN>;
close (COLUMN);
@sorted = sort { $a <=> $b } @not_sorted;
if (@vals % 2 == 0) {
$med = ($sorted[int($N/2)]);
print "MEDIAN = $med\n";
}
else {
$sum1 = $vals[(@vals/2)-1] + $vals[(@vals/2)];
$med = $sum1/2;
print "MEDIAN = $vals[@vals/2]\n";
};
感谢您的帮助。
I want to get the median of a specific column of data using a Perl Script,I got a script that reads, values from an array within the script,.
my (@vals, $med);
@vals =(12, 23, 34, 21, 66,66, 34, 87);
print "UNSORTED: @vals\n"; #sort data points
@vals = sort(@vals);
print "SORTED: @vals\n"; #test to see if there are an even number of data points
if( @vals % 2 == 0) { #if pair then:
$sum = $vals[(@vals/2)-1] + $vals[(@vals/2)];
$med = $sum/2;
print "The median value is $med\n";
}
else { #if odd then:
print "The median value is $vals[@vals/2]\n";
}
exit;
Can I transform this somehow to make it read the data from a file of several columns and calculate the median for a chosen column? like typing ./median.pl 1 column_numbers.tbl on the shell command.
I tried this, but the file data.txt has only one column
my (@vals, $med, $sum1, @numbers, @sorted);
open (COLUMN, "< data.txt") || die "Can not open file : $! ";
my @not_sorted = <COLUMN>;
close (COLUMN);
@sorted = sort { $a <=> $b } @not_sorted;
if (@vals % 2 == 0) {
$med = ($sorted[int($N/2)]);
print "MEDIAN = $med\n";
}
else {
$sum1 = $vals[(@vals/2)-1] + $vals[(@vals/2)];
$med = $sum1/2;
print "MEDIAN = $vals[@vals/2]\n";
};
Thanks for help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
原则上我同意 TLP 在这一点上的观点,但因为我目前没有发现关于 SO 的其他问题让我感兴趣:
只要数据以空格分隔,这就是有效的。第一个参数是 0 索引的列号,第二个参数是有效的文件名。
如果您是 Perl 新手,我可能应该注意到标量上下文中的数组返回元素的数量,例如
my $length = 2 - @column % 2;
和$median / = @medians;
。In principle I agree with TLP on this one, but since I found no other questions on SO that interested me at the moment:
This works, provided that the data is whitespace delimited. The first argument is the 0-indexed column number and the second is a valid filename.
If you're new to perl, I should probably note that arrays in scalar context returns the number of elements, like in
my $length = 2 - @column % 2;
and$median /= @medians;
.您对其余专栏感兴趣吗?如果没有,只需忽略它们即可。
此程序片段从文件中读取一行,然后将所需的列推入数组
@medium_array
中。由于大多数人将第一列视为第 #1 列,但 Perl 将其视为第 #0 列,因此我添加了一个名为COLUMN_OFFSET
的偏移量。主要工作是由这一行完成的:
该行使用
push
和split
命令。在您了解此行的作用之前,请不要继续。该程序可以使用一些更好的参数错误检查。用户请求的列是否大于文件中的列数?如果文件中不存在某列怎么办?如果文件不存在怎么办?所有这些东西都应该在这个程序中添加和检查。
最后,该程序将所需的列存储到
@medium_array
中。从那里,您应该能够使用以前的代码来查找该专栏的媒介。Are you interested in the rest of the columns? If not, simply ignore them.
This program snippet reads a line from a file, then pushes the wanted column into the array
@medium_array
. Since most people think of the first column as column #1, but Perl thinks of it as column #0, I've added an offset calledCOLUMN_OFFSET
.The main piece of work is done by this line:
This line uses the
push
, andsplit
commands. Do not move on until you understand what this line does.This program could use some better parameter error checking. Is the user requesting a column greater than the number of columns in the file? What if a column doesn't exist in the file? What if the file doesn't exist? All of this stuff should be added and checked in this program.
In the end, this program stores the desired column into
@medium_array
. From there, you should be able to use your previous code to find the medium for that column.