如何比较perl中的哈希值

如何比较perl中的哈希值,perl,Perl,我想在%instancesCD中搜索%instancessh的内容;具体而言,$file和$position键与关联值之间的匹配。如果%instancesCD包含来自%instancessh的键/值,则$fp将递增。如果%instancesCD不包含来自%instancessh的键/值,则$fn将递增。此外,如果我引用散列的方式不正确,我道歉;这是我第一次使用哈希 这就是我到目前为止所做的: #!/usr/bin/perl use warnings; use strict; print_acc

我想在
%instancesCD
中搜索
%instancessh
的内容;具体而言,
$file
$position
键与关联值之间的匹配。如果
%instancesCD
包含来自
%instancessh
的键/值,则
$fp
将递增。如果
%instancesCD
不包含来自
%instancessh
的键/值,则
$fn
将递增。此外,如果我引用散列的方式不正确,我道歉;这是我第一次使用哈希

这就是我到目前为止所做的:

#!/usr/bin/perl
use warnings;
use strict;

print_accuracy(determine_accuracy(determine_instancesTH(), determine_instancesCD()));

sub determine_accuracy {
my (%instancesTH, %instancesCD) = (@_);
    foreach my $file (keys %instancesTH) {
        foreach my $pos (keys %{$instancesTH{$file} } ) {
                if {
                # ... 
                    my $fp++;
                }
                else {
                    my $fn++;
                }
         }
     }
    return $tp, $fn;
}

sub print_accuracy {
    my ($tp, $fn) = (@_);
    print "True Positives: $tp\n";
    print "False Negatives: $fn\n\n";
}

sub determine_instancesA {
    # ... 
    return %instancesA;
}

sub determine_instancesB {
    # ... 
    return %instancesB;
}

谢谢。

您需要迭代其中一个密钥,并检查每个密钥是否存在。这是一个详细的实现

use strict;
use warnings;

my %foo = ( a => 1, b => 2, c => 3, x => 7 );
my %bar = ( x => 7, y => 8, z => 9 );

foreach my $key ( keys %bar ) {
    CORE::say $key if exists $foo{$key} && $bar{$key} eq $foo{$key};
}
这将打印
x
。如果从
%foo
中删除
x=>7
,它将不打印任何内容


下面是我们的聊天延长的结果。通过单元测试比较键和值的完整程序

use strict;
use warnings;
use Test::More;

my @test_cases = (
    {
        'name' => 'all keys match',
        'th'   => {
            surfacecharge => {
                87 => 'negatively charged',
            },
        },
        'cd' => {
            surfacecharge => {
                87 => 'negatively charged',
            },
        },
        'true positives'  => { surfacecharge => 1, }, # here 
        'false negatives' => { surfacecharge => 0, }, # but this one comes out undef
    },
    {
        'name' => 'key matches, value does not',
        'th'   => {
            surfacecharge => {
                87 => 'negatively charged',
            },
        },
        'cd' => {
            surfacecharge => {
                87 => 'positively charged',
            },
        },
        'true positives'  => { surfacecharge => 0, },
        'false negatives' => { surfacecharge => 1, },
    },
    {
        'name' => 'two matching keys',
        'th'   => {
            surfacecharge => {
                87 => 'negatively charged',
                88 => 'chronically tired',
            },
        },
        'cd' => {
            surfacecharge => {
                87 => 'negatively charged',
                88 => 'chronically tired',
            },
        },
        'true positives'  => { surfacecharge => 2, },
        'false negatives' => { surfacecharge => 0, },
    },
    {
        'name' => 'two/zero, one/one',
        'th'   => {
            surfacecharge => {
                87 => 'negatively charged',
                88 => 'chronically tired',
            },
            areasurcharge => {
                1 => 'stuff',
                2 => 'goo',
            },
        },
        'cd' => {
            surfacecharge => {
                87 => 'negatively charged',
                88 => 'chronically tired',
            },
            areasurcharge => {
                1 => 'stuff',
                0 => 'do not want',
            },
        },
        'true positives'  => { surfacecharge => 2, areasurcharge => 1, },
        'false negatives' => { surfacecharge => 0, areasurcharge => 1, },
    },
);

foreach my $test (@test_cases) {
    my ( $true_positives, $false_negatives ) = determine_accuracy( $test->{th}, $test->{cd} );
    is_deeply $true_positives,  $test->{'true positives'},  "$test->{name}: true positives";
    is_deeply $false_negatives, $test->{'false negatives'}, "$test->{name}: false negatives";
}
done_testing;

sub determine_accuracy {
    my ( $instancesTH, $instancesCD ) = @_;

    my $tp;
    my $fn;

    foreach my $file ( keys %{$instancesTH} ) {
        $tp->{$file} = 0;
        $fn->{$file} = 0;
        foreach my $pos ( keys %{ $instancesTH->{$file} } ) {
            if ( exists $instancesCD->{$file}->{$pos}
                && $instancesCD->{$file}->{$pos} eq $instancesTH->{$file}->{$pos} )
            {
                $tp->{$file}++;
            }
            else {
                $fn->{$file}++;
            }
        }
    }
    return $tp, $fn;
}

CD是否应包含任何TH值以使条件为真?即使每个键中有十个键,并且只有一个键/值对是相同的,您也应该始终
使用strict
。很好,您有
警告
,但也请打开
严格
。此外,您的
%instancesCD
将始终为空。不能像这样分配给两种列表类型(哈希或数组)。Perl只传递一个值列表,不知道在
@
中,一个列表结束,另一个开始。将引用传递给
确定精度
。您所有的函数名都是snake-case。为什么变量名是驼峰大小写?决定一个。Snake-case是大多数人在Perl.hello@simbabque中做的事情。我确实使用了strict,但它产生了一些错误,告诉我将哈希设置为全局。然后,当我将“我们的”替换为“我的”时,错误仍然存在,因此这是我阻止它的唯一方法。@simbabque,非常感谢您的帮助,我非常感谢。所以我试着检查TH中的每个键/值对,看看它是否存在于CD中。对于在CD中找到的每个TH键/值对,条件应为true;如果在CD中找不到TH键/值对,条件应为false。出现这种情况的次数应该很多,在1-5000之间,每次找到匹配项(或未找到匹配项)时,tp和fn值都应递增。这有意义吗?你一定要让它与
严格的
一起工作。看起来您还不了解作用域在Perl中是如何工作的。但是首先用
my(%InstanceTh,%instancesCD)=(@)解决问题和更新代码的问题。然后我们可以谈谈计数。