perl 按列统计 计算A,B,C,D列中 数字2 在每列中出现的百分率.
来源:学生作业帮 编辑:大师作文网作业帮 分类:综合作业 时间:2024/09/22 01:17:22
perl 按列统计 计算A,B,C,D列中 数字2 在每列中出现的百分率.
计算A,B,C,D列中 数字2 在每列中出现的百分率.
Name
A
B
C
D
sample1
1
2
3
4
sample2
1
2
3
4
sample3
1
2
3
4
sample4
3
2
8
4
sample5
1
2
3
2
sample6
1
5
3
4
sample7
1
2
3
4
sample8
1
2
3
4
sample9
1
2
2
4
sample10
1
2
3
2
sample11
2
2
3
4
sample12
1
2
3
2
sample13
1
2
3
4
计算A,B,C,D列中 数字2 在每列中出现的百分率.
Name
A
B
C
D
sample1
1
2
3
4
sample2
1
2
3
4
sample3
1
2
3
4
sample4
3
2
8
4
sample5
1
2
3
2
sample6
1
5
3
4
sample7
1
2
3
4
sample8
1
2
3
4
sample9
1
2
2
4
sample10
1
2
3
2
sample11
2
2
3
4
sample12
1
2
3
2
sample13
1
2
3
4
use strict;
# 设定
my $col_per_row = 5;
my $lookup_target = 2;
# my $lookup_target = "2"; # 如果你搜的是文字就用这句, 不要上面那句
#
my @fields;
foreach ( 1 .. $col_per_row ) {
my $field_name = <DATA>;
chomp $field_name;
push @fields, $field_name;
}
my @data = <DATA>;
chomp @data;
my (%hash, $total_rows) ;
while ( @data ) {
$total_rows++;
foreach my $col ( 0..$#fields ) {
$hash{$fields[$col]}[$total_rows] = shift @data;
}
}
foreach my $col_index ( 1.. $#fields ) {
print "target '$lookup_target' in col $fields[$col_index] = ";
my $sum;
foreach ( 1 .. $total_rows ) {
$sum ++ if $hash{$fields[$col_index]}[$_] == $lookup_target;
# $sum ++ if $hash{$fields[$col_index]}[$_] eq $lookup_target; # 如果你搜的是文字就用这句, 不要上面那句
}
my $percent;
$percent = sprintf "%.2f", ($sum / $total_rows * 100 );
print "$percent%$/";
}
__DATA__
Name
A
B
C
D
sample1
1
2
3
4
sample2
1
2
3
4
sample3
1
2
3
4
sample4
3
2
8
4
sample5
1
2
3
2
sample6
1
5
3
4
sample7
1
2
3
4
sample8
1
2
3
4
sample9
1
2
2
4
sample10
1
2
3
2
sample11
2
2
3
4
sample12
1
2
3
2
sample13
1
2
3
4
# 设定
my $col_per_row = 5;
my $lookup_target = 2;
# my $lookup_target = "2"; # 如果你搜的是文字就用这句, 不要上面那句
#
my @fields;
foreach ( 1 .. $col_per_row ) {
my $field_name = <DATA>;
chomp $field_name;
push @fields, $field_name;
}
my @data = <DATA>;
chomp @data;
my (%hash, $total_rows) ;
while ( @data ) {
$total_rows++;
foreach my $col ( 0..$#fields ) {
$hash{$fields[$col]}[$total_rows] = shift @data;
}
}
foreach my $col_index ( 1.. $#fields ) {
print "target '$lookup_target' in col $fields[$col_index] = ";
my $sum;
foreach ( 1 .. $total_rows ) {
$sum ++ if $hash{$fields[$col_index]}[$_] == $lookup_target;
# $sum ++ if $hash{$fields[$col_index]}[$_] eq $lookup_target; # 如果你搜的是文字就用这句, 不要上面那句
}
my $percent;
$percent = sprintf "%.2f", ($sum / $total_rows * 100 );
print "$percent%$/";
}
__DATA__
Name
A
B
C
D
sample1
1
2
3
4
sample2
1
2
3
4
sample3
1
2
3
4
sample4
3
2
8
4
sample5
1
2
3
2
sample6
1
5
3
4
sample7
1
2
3
4
sample8
1
2
3
4
sample9
1
2
2
4
sample10
1
2
3
2
sample11
2
2
3
4
sample12
1
2
3
2
sample13
1
2
3
4
perl 按列统计 计算A,B,C,D列中 数字2 在每列中出现的百分率.
EXCEL中A列是数字其中有重复的,B列是从小到大排列1,2,3,4,5,6.要求C列中显示B列中数字在A列中出现的次数
急求如何用Excel将A列同时与条件列:B列,C列,D列等列求交集,并将其结果按列分别表示在其它列中,即比如结果列:Q列
IF语句C列中某个单元格的内容包含在B列中,那么D列等于B列中对应的A列内容
EXCEL中,B列中包含A列的数字,怎么才能在C列中显示出个数?
C列中包含B列的值,在A列中显示C列对应的D列的数据?求教,
excel中计算公式怎么弄?比如A列上的数字减B列上的数字要在C列上显示,怎么弄?
在EXCEL中怎样根据ABC列中的数字得到D列中的字母,其中B列包含C中的数字,但A列是和B列一一对应的
excel表格中,如果C列数值等于A列中某个数值,则在C列对应的D列中返回A列对应的B列数值,D列怎么设公式呢
EXCEL表中如何计算两列数值出现次数之差?比如:A 列中数字为 1 2 3 4 4 5 6 6 7 B列中为A列数字的
excel A列 B列 C列 三列数据每列取一个数据排列 显示在D列
excel中“A列乘以B列”减去“C列乘以D列”,再累加每一行的差值,函数应该怎么写?