有没有一种简单的方法来验证哈希元素比较的哈希值?
有没有一种简单的方法来验证哈希元素比较的哈希值?
我需要验证哈希元素 $Table{$key1}{$key2}{K1}{Value} 的 Perl 哈希,与哈希中的所有其他元素进行比较,
第三个键将是 k1 到 kn,我想比较这些元素和其他键是相同的
if ($Table{$key1}{$key2}{K1}{Value} eq $Table{$key1}{$key2}{K2}{Value}
eq $Table{$key1}{$key2}{K3}{Value} )
{
#do whatever
}
Is there a simple way to validate a hash of hash element comparsion ?
I need to validate a Perl hash of hash element $Table{$key1}{$key2}{K1}{Value} compare to all other elements in hash
third key will be k1 to kn and i want comprare those elements and other keys are same
if ($Table{$key1}{$key2}{K1}{Value} eq $Table{$key1}{$key2}{K2}{Value}
eq $Table{$key1}{$key2}{K3}{Value} )
{
#do whatever
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
像这样的事情可能会起作用:
Something like this may work:
我会使用 Data::Dumper 来帮助完成这样的任务,特别是对于更一般的问题(其中第三个键比“K1”...“Kn”更任意)。使用 Data::Dumper 将数据结构字符串化,然后比较字符串。
这可用于更一般的情况,其中
$Table{$key1}{$key2}{k7}{value}
本身包含复杂的数据结构。然而,当检测到差异时,它并不能帮助您找出差异在哪里。I would use
Data::Dumper
to help with a task like this, especially for a more general problem (where the third key is more arbitrary than 'K1'...'Kn'). UseData::Dumper
to stringify the data structures and then compare the strings.This can be used for the more general case where
$Table{$key1}{$key2}{k7}{value}
itself contains a complex data structure. When a difference is detected, though, it doesn't give you much help figuring out where that difference is.相当复杂的结构。您应该考虑使用面向对象的编程技术。这将大大简化您的编程和这些复杂结构的处理。
首先,让我们简单一点。当你说:
你真的是说:
或者
我会假设第一个。如果我错了,请告诉我,我会更新我的答案。
让我们简化一下:
现在,我们只处理哈希。您可以使用两种技术:
这是第一个技术:
第二个:
A fairly complex structure. You should be looking into using object oriented programming techniques. That would greatly simplify your programming and the handling of these complex structures.
First of all, let's simplify a bit. When you say:
Do you really mean:
or
I'm going to assume the first one. If I'm wrong, let me know, and I'll update my answer.
Let's simplify:
Now, we're just dealing with a hash. There are two techniques you can use:
Here's the first technique:
And the second:
解决这个问题的另一种方法是创建实用程序函数,如果所有键的某个函数返回的值相同,它将比较所有键:
但是如果您开始以这种方式思考,您会发现这种方法更加通用(推荐方式):
如果映射操作很昂贵,您可以制作相同的惰性对应项:
如果您只想要键的某些子集,您可以使用哈希切片语法,例如:
Alternative approach to the problem is make utility function which will compare all keys if has same value returned from some function for all keys:
But if you start thinking in this way you can found this approach much more generic (recommended way):
If mapping operation is expensive you can make lazy counterpart of same:
If you want only some subset of keys you can use hash slice syntax e.g.: