需要帮助折叠列表并获取perl中的总计

时间:2010-11-24 15:56:34

标签: arrays perl file hash

您好我有大量数据: http://paste-it.net/public/y17027d/ 它是67859行乘10列。第6列包含表示从1到6的Z-Scores的值,以.01为增量。我想要做的是总计具有相同Z得分值的所有其他列值,但我当前的代码不起作用。

我现在打印出的值但每个Z-Score的总数不正确 这是我的代码:

#! /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);

我知道我必须做错事但我无法弄清楚是什么。任何帮助将不胜感激。谢谢

2 个答案:

答案 0 :(得分:2)

行。我能够从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;

答案 1 :(得分:1)

我想你想说

$false_positives_new = $false_positives_new + $fp;
# etc.

而不是

$false_positives_new = $false_positives + $fp;