将两个散列与键和值进行比较 [英] Comparing two hashes with the keys and values

查看:50
本文介绍了将两个散列与键和值进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想比较两个散列,首先看看它们是否存在于第一个散列中,是否存在于第二个散列中,如果是,则比较值并打印成功,否则如果值不相等,则打印键具有不平等的价值.我已经解决了一些现有的类似问题,但它让我感到困惑.希望我能得到帮助.

I want to compare two hashes, first to see if they key which is in the 1st hash, exists in the 2nd hash and if so compare the values and print a success else if the values are not equal, print the key which has the unequal value.I have gone through some existing similar questions but it confuses me.Hope i can get help.

推荐答案

以下内容应该可以帮助您了解:

The following should help you get an idea:

for ( keys %hash1 ) {
    unless ( exists $hash2{$_} ) {
        print "$_: not found in second hash\n";
        next;
    }

    if ( $hash1{$_} eq $hash2{$_} ) {
        print "$_: values are equal\n";
    }
    else {
        print "$_: values are not equal\n";
    }
}

这篇关于将两个散列与键和值进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆