Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
散列中的Perl计数器_Perl_Hash - Fatal编程技术网

散列中的Perl计数器

散列中的Perl计数器,perl,hash,Perl,Hash,我有一个这样的文件: 1 1 A12P P1234 1 0 A52Q P1234 1 1 M12P P8866 1 1 R50T P1222 1 1 A82L P8866 0 0 D83F P8866 .... 在perl循环中,我解析每一行并保存每一列值: while (<FILE>){ ... my $actual=$1; my $predicted=$2; $mutation=$3; $id=$4; i

我有一个这样的文件:

1   1  A12P P1234
1   0  A52Q P1234
1   1  M12P P8866
1   1  R50T P1222
1   1  A82L P8866
0   0  D83F P8866
....
在perl循环中,我解析每一行并保存每一列值:

while (<FILE>){
     ...
     my $actual=$1; my $predicted=$2; $mutation=$3; $id=$4; 
        if ($actual==1) {
            if ($predicted==1)   { $hash{$id}{$truep}++; } 
            elsif ($predicted==0){ $hash{$id}{$falsen}++; } 
        } elsif ($actual==0) {
            if ($predicted==0)   { $hash{$id}{$truen}++; } 
            elsif ($predicted==1){ $hash{$id}{$falsep}++; } 
        }
}

for $id (keys %hash) {
        $tp = $hash{$id}{$truep};
        $tn = $hash{$id}{$truen};
        $fp = $hash{$id}{$falsep};
        $fn = $hash{$id}{$falsen};
        ...
    }
我希望,对于P1234:

$hash{$id}{$fp} --> FP = 0
$hash{$id}{$tp} --> TP = 1
$hash{$id}{$fn} --> FN = 1
$hash{$id}{$tn} --> TN = 0

但是它没有给我期望的值。我是否错误地定义了散列?是否有更好的方法来解析文件?

假设您真的希望tn等是常量而不是变量,那么您需要为每个id初始化所有4,以便在散列中得到0

use warnings;
use strict;
use Data::Dumper;
$Data::Dumper::Sortkeys=1;

my %hash;
while (<DATA>){
    chomp;
    my ($actual, $predicted, $mutation, $id) = split;
    for my $type (qw(tn tp fn fp)) {
        $hash{$id}{$type} = 0 unless exists $hash{$id}{$type}; 
    }
    if (($actual == 1) and ($predicted == 1)) {
        $hash{$id}{tp}++;
    }
    elsif (($actual == 1) and ($predicted == 0)) {
        $hash{$id}{fn}++;
    }
    elsif (($actual == 0) and ($predicted == 0)) {
        $hash{$id}{tn}++;
    }
    elsif (($actual == 0) and ($predicted == 1)) {
        $hash{$id}{fp}++;
    }
}

print Dumper(\%hash);

__DATA__
1   1  A12P P1234
1   0  A52Q P1234
1   1  M12P P8866
1   1  R50T P1222
1   1  A82L P8866
0   0  D83F P8866

问题中包含的信息不足,无法帮助调试问题。您使用什么模式来捕获
$1
$2
$3
$4
的值?另外,您希望输出是什么?
$truep
$falsen
的值是什么?该文件实际上更复杂,这就是我没有写下模式的原因。但我之前检查过它,它工作正常。每个元素都被识别。但我认为计数器在某种程度上是在添加值,而没有考虑
$id
键。但我们无法判断,鉴于这个样本,是否发生了这种情况,或者它是如何发生的。我猜您遇到了选择/转换问题。(例如,检查前两个值是否符合预期,模式是否正确定位等)
use warnings;
use strict;
use Data::Dumper;
$Data::Dumper::Sortkeys=1;

my %hash;
while (<DATA>){
    chomp;
    my ($actual, $predicted, $mutation, $id) = split;
    for my $type (qw(tn tp fn fp)) {
        $hash{$id}{$type} = 0 unless exists $hash{$id}{$type}; 
    }
    if (($actual == 1) and ($predicted == 1)) {
        $hash{$id}{tp}++;
    }
    elsif (($actual == 1) and ($predicted == 0)) {
        $hash{$id}{fn}++;
    }
    elsif (($actual == 0) and ($predicted == 0)) {
        $hash{$id}{tn}++;
    }
    elsif (($actual == 0) and ($predicted == 1)) {
        $hash{$id}{fp}++;
    }
}

print Dumper(\%hash);

__DATA__
1   1  A12P P1234
1   0  A52Q P1234
1   1  M12P P8866
1   1  R50T P1222
1   1  A82L P8866
0   0  D83F P8866
$VAR1 = {
          'P1222' => {
                       'fn' => 0,
                       'fp' => 0,
                       'tn' => 0,
                       'tp' => 1
                     },
          'P1234' => {
                       'fn' => 1,
                       'fp' => 0,
                       'tn' => 0,
                       'tp' => 1
                     },
          'P8866' => {
                       'fn' => 0,
                       'fp' => 0,
                       'tn' => 1,
                       'tp' => 2
                     }
        };