Arrays 需要帮助在perl中折叠列表并获取总数吗
嗨,我有一个大的数据列表: 它是67859行10列。第6列包含以.01为增量表示从1到6的Z分数的值。我想做的是合计具有相同Z值的所有其他列值,但我当前的代码不起作用 我现在打印出的值,但每个Z分数的总数不正确。Arrays 需要帮助在perl中折叠列表并获取总数吗,arrays,perl,file,hash,Arrays,Perl,File,Hash,嗨,我有一个大的数据列表: 它是67859行10列。第6列包含以.01为增量表示从1到6的Z分数的值。我想做的是合计具有相同Z值的所有其他列值,但我当前的代码不起作用 我现在打印出的值,但每个Z分数的总数不正确。 这是我的密码: #! /usr/bin/perl use strict; use warnings; use POSIX; use Data::Dumper; my $input = $ARGV[0]; open (DATAFILE, $input) or die $!; op
这是我的密码:
#! /usr/bin/perl
use strict;
use warnings;
use POSIX;
use Data::Dumper;
my $input = $ARGV[0];
open (DATAFILE, $input) or die $!;
open(OUT,">>"."final.output.txt");
my($line,$fMeasure,$filename,$recall,$precision,$z_score,$computer_calls,$johns_calls,$false_negatives,$false_positives,$true_positives,$count);
$fMeasure=$filename=$recall=$precision=$z_score=$computer_calls=$johns_calls=$false_negatives=$false_positives=$true_positives=$count = 0;
my %stats=();
my %zscore=();
while($line = <DATAFILE>){
# Chop off new line character, skip the comments and empty lines.
chomp($line);
my @temp = split(/\t/, $line);
$true_positives = $temp[0];
$false_positives = $temp[1];
$false_negatives = $temp[2];
$johns_calls = $temp[3];
$computer_calls = $temp[4];
$z_score = $temp[5];
$fMeasure = $temp[6];
$precision = $temp[7];
$recall = $temp[8];
$filename = $temp[9];
$stats{$z_score}{$filename}[0] = $true_positives;
$stats{$z_score}{$filename}[1] = $false_positives;
$stats{$z_score}{$filename}[2] = $johns_calls;
$stats{$z_score}{$filename}[3] = $computer_calls;
$stats{$z_score}{$filename}[4] = $fMeasure;
$stats{$z_score}{$filename}[5] = $precision;
$stats{$z_score}{$filename}[6] = $recall;
$stats{$z_score}{$filename}[6] = $filename;
$zscore{$z_score}++;
}
my $false_positives_new = 0;
my $true_positives_new = 0;
my $johns_calls_new = 0;
my $computer_calls_new = 0;
my $file_name = 0;
foreach $z_score ( sort keys %stats ) {
foreach $filename( keys %{$stats{$z_score}} ){
my $tp = $stats{$z_score}{$filename}[0];
my $fp = $stats{$z_score}{$filename}[1];
my $jc = $stats{$z_score}{$filename}[2];
my $cc = $stats{$z_score}{$filename}[3];
my $fn = $stats{$z_score}{$filename}[6];
#print "$z_score\t$jc\n";
$false_positives_new = $false_positives + $fp;
$true_positives_new = $true_positives + $tp;
$johns_calls_new = $johns_calls + $jc;
$computer_calls_new = $computer_calls + $cc;
#print OUT "$fn\n";
}
print OUT"$true_positives_new\t$false_positives_new\t$johns_calls_new\t$computer_calls_new\t$z_score \n";
$false_positives_new = 0;
$true_positives_new = 0;
$johns_calls_new = 0;
$computer_calls_new = 0;
$file_name = 0;
}
close(OUT);
close (DATAFILE);
#/usr/bin/perl
严格使用;
使用警告;
使用POSIX;
使用数据::转储程序;
my$input=$ARGV[0];
打开(数据文件,$input)或死亡$!;
打开(OUT,“>>”“final.output.txt”);
我的($line、$fMeasure、$filename、$recall、$precision、$z_score、$computer_calls、$johns_calls、$false_negations、$false_posities、$true_posities、$count);
$fMeasure=$filename=$recall=$precision=$z_score=$computer_calls=$johns_calls=$false_negatives=$false_positives=$true_positives=$count=0;
我的%stats=();
我的%zscore=();
而($line=){
#切掉新行字符,跳过注释和空行。
chomp($line);
my@temp=拆分(/\t/,$line);
$true_positives=$temp[0];
$false_positives=$temp[1];
$false_negatives=$temp[2];
$johns_calls=$temp[3];
$computer_calls=$temp[4];
$z_分数=$temp[5];
$fMeasure=$temp[6];
$precision=$temp[7];
$recall=$temp[8];
$filename=$temp[9];
$stats{$z_score}{$filename}[0]=$true_正值;
$stats{$z_score}{$filename}[1]=$false_positives;
$stats{$z_score}{$filename}[2]=$johns_calls;
$stats{$z_score}{$filename}[3]=$computer_calls;
$stats{$z_score}{$filename}[4]=$fMeasure;
$stats{$z_score}{$filename}[5]=$precision;
$stats{$z_score}{$filename}[6]=$recall;
$stats{$z_score}{$filename}[6]=$filename;
$zscore{$z_分数}++;
}
我的$false\u positives\u new=0;
我的$true\u positives\u new=0;
我的$johns\u calls\u new=0;
我的$computer\u calls\u new=0;
我的$file\u name=0;
foreach$z_分数(排序键%stats){
foreach$filename(键%{$stats{$z_score}}){
my$tp=$stats{$z_score}{$filename}[0];
my$fp=$stats{$z_score}{$filename}[1];
my$jc=$stats{$z_score}{$filename}[2];
my$cc=$stats{$z_score}{$filename}[3];
my$fn=$stats{$z_score}{$filename}[6];
#打印“$z_分数\t$jc\n”;
$false_positives_new=$false_positives+$fp;
$true\u positives\u new=$true\u positives+$tp;
$johns_calls_new=$johns_calls+$jc;
$computer\u calls\u new=$computer\u calls+$cc;
#打印出“$fn\n”;
}
打印出“$true\u positives\u new\t$false\u positives\u new\t$johns\u calls\u new\t$computer\u calls\u new\t$z_score\n”;
$false\u positives\u new=0;
$true\u positives\u new=0;
$johns_calls_new=0;
$computer\u calls\u new=0;
$file_name=0;
}
收尾;
关闭(数据文件);
我知道我一定做错了什么,但我不知道是什么。任何帮助都将不胜感激。谢谢好的。我能够从pastebin中获取数据,我认为下面的代码可以满足您的需要
#! /usr/bin/perl
use strict; use warnings;
use Data::Dumper;
my ($input) = @ARGV;
open my $DATAFILE, '<', $input
or die "Cannot open '$input': $!";
my @field_names = qw(
fMeasure
recall
precision
z_score
computer_calls
johns_calls
false_negatives
false_positives
true_positives
count
);
my @track_fields = qw(
false_positives
false_negatives
johns_calls
computer_calls
);
my (%stats, %by_zscore);
while ( my $line = <$DATAFILE> ) {
last unless $line =~ /\S/;
chomp $line;
my @temp = split /\t/, $line;
my $filename = pop @temp;
my %fields;
@fields{ @field_names } = @temp;
my $z_score = $fields{z_score};
$stats{ $z_score }{$filename} = \@temp;
for my $f ( @track_fields ) {
$by_zscore{$z_score}{ $f } += $fields{ $f };
}
}
print Dumper \%by_zscore;
#/usr/bin/perl
严格使用;使用警告;
使用数据::转储程序;
我的($input)=@ARGV;
打开我的$DATAFILE,“确定。我能够从pastebin中获取数据,我认为下面的代码可以满足您的需要
#! /usr/bin/perl
use strict; use warnings;
use Data::Dumper;
my ($input) = @ARGV;
open my $DATAFILE, '<', $input
or die "Cannot open '$input': $!";
my @field_names = qw(
fMeasure
recall
precision
z_score
computer_calls
johns_calls
false_negatives
false_positives
true_positives
count
);
my @track_fields = qw(
false_positives
false_negatives
johns_calls
computer_calls
);
my (%stats, %by_zscore);
while ( my $line = <$DATAFILE> ) {
last unless $line =~ /\S/;
chomp $line;
my @temp = split /\t/, $line;
my $filename = pop @temp;
my %fields;
@fields{ @field_names } = @temp;
my $z_score = $fields{z_score};
$stats{ $z_score }{$filename} = \@temp;
for my $f ( @track_fields ) {
$by_zscore{$z_score}{ $f } += $fields{ $f };
}
}
print Dumper \%by_zscore;
#/usr/bin/perl
严格使用;使用警告;
使用数据::转储程序;
我的($input)=@ARGV;
打开我的$DATAFILE,“我想你想说
$false_positives_new = $false_positives_new + $fp;
# etc.
而不是
$false_positives_new = $false_positives + $fp;
我想你想说
$false_positives_new = $false_positives_new + $fp;
# etc.
而不是
$false_positives_new = $false_positives + $fp;
Math::Complex
在那里做什么?现在什么都没有,但我很可能会在以后修改程序并需要它。但这不应影响代码的功能。我很难理解数据制表中的复杂数字,但无论如何。开始时的巨大变量声明/初始化行非常难看,完全没有必要。@Alos:我会将所有这些值存储在哈希表中,而不是在单独的标量变量中,只返回函数中的一个散列作为所有统计分析的结果,例如,my%数据;foreach。。。。{@data{qw(tp fp jb cc fn)}={$stats{$z_score}{$filename}[0,1,2,3,6];$data{false_posities}+=$fp;..}
数学::复杂
在那里做什么?现在什么都没有,但我很可能会在以后修改程序并需要它。但这不应影响代码的功能。我很难理解数据制表中的复杂数字,但无论如何。开始时的巨大变量声明/初始化行非常难看,完全没有必要。@Alos:我会将所有这些值存储在哈希表中,而不是在单独的标量变量中,只返回函数中的一个散列作为所有统计分析的结果,例如,my%数据;foreach。。。。{@data{qw(tp fp jb cc fn)}={$stats{$z_score}{$filename}}[0,1,2,3,6];$data{false_positives}+=$fp;..}
@Alos:我希望这并不意味着你将继续使用这些变量而不是哈希。@Sinan不,我已经将它修改为使用哈希。Thanks@Alos:我希望这并不意味着你将继续使用这些变量而不是散列。@Sinan不,我已经将其修改为使用散列。谢谢