简体   繁体   English

如何使用Perl比较两组哈希引用?

[英]How can I compare two sets of hash references using Perl?

So I have two files with similar information. 因此,我有两个信息相似的文件。

EXAMPLES 例子

test.txt 的test.txt
1=1 1 = 1
2=2 2 = 2
3=3 3 = 3

test_2.txt test_2.txt
1=1 1 = 1
2=4 2 = 4
3=5 3 = 5
4=4 4 = 4
5=5 5 = 5
6=6 6 = 6

Now I want to print each hashes keys and values. 现在我想打印每个哈希键和值。

my %hash1;  
my $scalar_value1;  
my $scalar_value2;  
my $file = "/test/test.txt";  
open (TEST, "<$file") or die "$!";  
while (TEST) {  
            ($scalar_value1, $scalar_value2) = split( '=' );  
            $hash1{$scalar_value1}{'value1'} = $scalar_value1;  
            $hash1{$scalar_value1}{'value2'} = $scalar_value2;  
        }  
close TEST;  

foreach my $scalar_value1 (sort keys %hash1) {  
        print "$hash1{$scalar_value1}{'value1'} | $hash1{$scalar_value1}{'value2'}";  
}  


my %hash2;  
my $scalar_value_1_2;  
my $scalar_value_2_2;  
my $file_2 = "/test/test2.txt";  
open (TEST_2, "<$file_2") or die "$!";  
while (TEST_2) {  
            ($scalar_value_1_2, $scalar_value_2_2) = split( '=' );  
            $hash1{$scalar_value_1_2}{'value_1_2'} = $scalar_value_1_2;  
            $hash1{$scalar_value_1_2}{'value_2_2'} = $scalar_value_2_2;  
        }  
close TEST_2;  

foreach my $scalar_value_1_2 (sort keys %hash1) {  
        print "$hash1{$scalar_value_1_2}{'value1_2'} | $hash1{$scalar_value_1_2}{'value1_2'}";  
}    

Now how can I compare the two hashes to produce a new value based on whether or not the first hash contained a similar key? 现在,如何根据第一个哈希值是否包含相似的密钥来比较两个哈希值以产生新值?

if ($hash1{$scalar_value_1_2}{'value1_2} eq $hash1{$scalar_value1}{'value1'}) {  
    my $scalar_value_2_2; = $hash1{$scalar_value1}{'value2'};  
    print "YES MATCH: $scalar_value_2_2\n";  
} else {  
    print "N0 MATCH: $scalar_value_2_2\n";  
}  

I rewrote your program for better clarity. 我重写了您的程序,以使其更加清晰。 "chomp" is important to include to remove new line. “ chomp”很重要,以包括删除新行。

use strict;
use warnings;
sub read_hash {
    my $fname = shift;
    open (my $fh, "<",$fname) or die "$!";  
    my %hash;
    while (<$fh>) {
        chomp;
        my ($key,$value)=split /=/;
        $hash{$key}=$value;
    }
    return %hash;
}

my %hash1=read_hash("test.txt");

foreach my $key (sort keys %hash1) {  
        print "$key = $hash1{$key}\n";
}  

my %hash2=read_hash("test1.txt");

foreach my $key (sort keys %hash2) {  
        print "$key = $hash2{$key}\n";
}  


#------------------------

foreach my $key (sort keys %hash2) {  
        if (exists $hash1{$key}) {
            print "$key exists in first hash\n";
        } else {
            print "$key does not exist in first hash\n";
        }
}  

Sounds like a simple set intersection problem. 听起来像是一个简单的集合交集问题。 Here's a reshuffled version of the code using Alexandr's work. 这是使用Alexandr的工作重新整理的代码版本。

use strict;
use warnings;
sub read_hash {
    my $fname = shift;
    open (my $fh, "<",$fname) or die "$!";  
    my %hash;
    while (<$fh>) {
        chomp;
        my ($key,$value)=split /=/;
        $hash{$key}=$value;
    }
    # let's retrun the reference here. With big hashes, you want to avoid copying
    return \%hash;
}

my $h1=read_hash("test.txt");
my $h2=read_hash("test1.txt");

map {print "$_ = $h1->{$_}\n"} sort keys %$h1;
map {print "$_ = $h2->{$_}\n"} sort keys %$h2;

map {print "key from h1 $_ exists in h2\n" if exists $h2->{$_} } sort keys %$h1; 

# if you just want to take out the items from $h2 that also exists in $h1.
my %h3 = map {$_=>$h2->{$_} if exists $h2->{$_}} keys %$h1;

Notice that the program uses map to do list iteration. 请注意,程序使用map来执行列表迭代。 The last usage of map creates a hash that contains the intersection of the keys of the 2 hashes. map的最后一个用法是创建一个包含2个哈希键的交集的哈希。 In order to optimize the code further, I would iterate through the hash that has less number of keys. 为了进一步优化代码,我将遍历具有较少密钥数量的哈希。 Also note that the methods above does not check the value of the keys. 另请注意,上述方法不会检查键的值。 It only compares the keys themselves. 它只比较键本身。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM