比较Perl中多个哈希值的键值

时间:2014-01-16 16:15:41

标签: perl hash comparison

我有一组哈希(使用Tie :: IxHash保留键序列),我想比较每个键的值。哈希的数量可以变化。例如,我想计算键“1”分配值“1”和“0”的次数。我知道如果我将哈希值放在哈希数组中然后遍历它们,那么应该有一个很好的快速计算值匹配的方法,但是我被困住了,无法自己解决。

%hash1 = ( 1 => '1',
           2 => '1',
           3 => '0',
           4 => '0',
         );

%hash2 = ( 1 => '1',
           2 => '1',
           3 => '1',
           4 => '0',
         );

%hash3 = ( 1 => '1',
           2 => '1',
           3 => '0',
           4 => '1',
         );

%hash4 = ( 1 => '1',
           2 => '0',
           3 => '0',
           4 => '1',
         );

上述目标的结果是:

$key1: $agr1 = 4; $agr0 = 0;
$key2: $agr1 = 3; $agr0 = 1;
$key3: $agr1 = 1; $agr0 = 3;
$key4: $agr1 = 2; $agr0 = 2;

现在我最终做的是循环第一个哈希的键,然后将它们与其他每个哈希值进行比较,这显然是单调乏味和愚蠢的。

感谢您的帮助!

更正:我正在使用哈希,而不是哈希引用。相应地编辑了上面的代码。

3 个答案:

答案 0 :(得分:2)

这样可以在结果中提供一点灵活性,因为您可以在@wanted中添加任何所需的值。假设您的哈希实际上是哈希引用(在您的示例中不明确):

my @hashes = ($hash1,$hash2,$hash3,$hash4);
my %count;
for my $hash (@hashes) {
    $count{ $_ }{ $hash->{$_} }++ for keys %$hash;
}
my @wanted = (1,0);
for my $key (sort { $a <=> $b } keys %count) {
    my @result = map { "agr$_ = " . ($count{$key}{$_} || 0) . ';' } @wanted;
    print "key $key: @result\n";
}

输出:

key 1: agr1 = 4; agr0 = 0;
key 2: agr1 = 3; agr0 = 1;
key 3: agr1 = 1; agr0 = 3;
key 4: agr1 = 2; agr0 = 2;

答案 1 :(得分:1)

Pseudoperl:

# Build a list of refs to your hashes
@a = { \%hash1, \%hash2, ... }

# A container to hold the keys and counts
$keys = {} 

# Initialize the key container
for my $h in @a
    for my $k in %$h
        $keys->{$k} = {0 => 0, 1 => 0} unless exists $keys->{$k}

# Iterate once over all the keys and count occurrences
# assumes input hash values can be only 0 or 1
for my $k in %$keys
    for my $h in @a
        $keys->{$k}->{$h->{$k}}++ if exists $h->{$k};

答案 2 :(得分:0)

首先,您的哈希示例是错误的。 %hash = {}

如果您使用% sigil,则需要()。如果你想要一个hash-ref,它将是$hash = {}

回到你的问题。你可以这样做。

这通常被称为“看见”哈希。

# appending your example above....
my @arr = (\%hash1, \%hash2, \%hash3, \%hash4);
my $seen = {}; 

# iterate over each hash
for my $hash (@arr) {
    # iterate over each key in hash
    for my $k (keys %$hash) {
        my $val = $hash->{$k};

        # check $val and increment 
        if ($val) {
            $seen->{$k}{1}++;
        } else {
            $seen->{$k}{0}++;
        }   
    }   
}

for my $k ( sort keys %$seen ) { 
    # in case any value is 0/undef
    print "$k:  1 = "
      . ( $seen->{$k}->{0} ? $seen->{$k}->{0} : 0 ) . " 0 = "
      . ( $seen->{$k}->{0} ? $seen->{$k}->{0} : 0 ) . "\n";
}

哪个输出:

$ perl test.pl
1:  1 = 0 0 = 0
2:  1 = 1 0 = 1
3:  1 = 3 0 = 3
4:  1 = 2 0 = 2