【问题标题】:How to compare the inner hash values of following HoH with those of the other list如何比较以下HoH的内部哈希值与其他列表的内部哈希值
【发布时间】:2013-05-23 17:11:07
【问题描述】:
my %main_hash = ( 
    'hash1' => {
        'key1' => '1-111',
        'key2' => '1-222',
        'key3' => '1-333'
    },  
    'hash2' => {
        'key1' => '2-111',
        'key2' => '2-222',
        'key3' => '2-333'
    }   
);

如何将内部哈希(hash1)的键值(1-111,1-222,1-333)与以下列表的对应值进行比较:

$list=   thekey1is : 1-111
         thekey2is : 1-222
         thekey3is : 1-333;

并为每次比较的结果维护一个标志?

【问题讨论】:

  • 要比较的值是变量$list中hash1和":"后面的值。

标签: perl hash hash-of-hashes


【解决方案1】:

你可以这样做

#get all the keys
my @keys = keys %{ $main_hash{hash1} };

#check for each key
for my $key (@keys) {

    if  ($main_hash{hash1}{$key} == $main_hash{hash2}{$key}){
        print "$main_hash{hash1}{$key} and $main_hash{hash2}{$key} Match \n",
    }
    else{
        print "$main_hash{hash1}{$key} and $main_hash{hash2}{$key} Do not match \n",
    }


}

【讨论】:

  • 你的意思是%main_hash = ( ... )
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-03
  • 2013-06-06
  • 1970-01-01
  • 1970-01-01
  • 2015-01-25
  • 2016-06-27
相关资源
最近更新 更多