我该如何做我的 Perl 家庭作业?
给定一个perl哈希结构
{
'A' => {
'B' => 'C',
'D' => 'E'
},
'F' => {
'B' => 'G',
'D' => 'H'
},
'I' => {
'B' => 'G',
'D' => 'H'
},
'J' => {
'B' => 'C',
'D' => 'F'
},
}
}
,我需要检查重复的 F ,I 基于其 G 和 H 的内部配对(G 和 H 分别对于 F 和 I 中的 B 和 D 是常见的,(它们构成一个常见的重复对)
最终输出计数结构是这样的:
{
'B' => { 'C' => 2 ,'G' => 1} # see G's and H's count is 1 Taking G and H's pair only once. C is 2 because C, E and C,F do not make a pair, C comes twice and E and F once
'D' => { 'E' => 1, 'H' => 1, 'F'=>1, } # see H's count is 1
}
Perl中有没有快速的方法来做到这一点?
Given a perl hash structure
{
'A' => {
'B' => 'C',
'D' => 'E'
},
'F' => {
'B' => 'G',
'D' => 'H'
},
'I' => {
'B' => 'G',
'D' => 'H'
},
'J' => {
'B' => 'C',
'D' => 'F'
},
}
}
I need to check for duplicate F ,I based on its inner pairing of G and H (G and H is common for B and D respectively in F and I, (They make a common duplicate pair)
The final output count structure is like this:
{
'B' => { 'C' => 2 ,'G' => 1} # see G's and H's count is 1 Taking G and H's pair only once. C is 2 because C, E and C,F do not make a pair, C comes twice and E and F once
'D' => { 'E' => 1, 'H' => 1, 'F'=>1, } # see H's count is 1
}
Is there any fast way in perl to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设您想从 $hoh 中删除重复项并且二级结构并非偶然,您可以使用类似以下内容的内容:
输出:
Assuming you want to prune duplicates from $hoh and the two level structure isn't accidential, you could use something like:
output:
首先创建一个方法来告诉您哈希值是否相同。我不会自己编写这个,而是将其从另一个模块中拉出来 - 我将只使用
Test::More
中的eq_hash
,然后我们需要的只是一点一些 Perl 代码。而且,就是这样!
First create a method to tell you whether or not your hashes are the same. Rather than writing this myself I'll just yank it out of another module -- I'll just use
eq_hash
fromTest::More
, then all we need is a little bit of Perl code.And, that's it!
非常简单直接的解决方案:
Very simple and straightforward solution: