当键和值都是数组引用时的 Perl 哈希
我遇到一个问题,数字对映射到其他数字对。例如,(1,2)→(12,97)。有些对可能映射到多个其他对,所以我真正需要的是能够将一对映射到列表列表中,例如 (1,2)->((12,97),(4,1))。归根结底,我想单独处理每个值(即每个列表列表)。
在 Python 中,我可以通过简单地说:
key = ( x, y )
val = [ a, b ]
if (x,y) not in my_dict:
my_dict[ (x,y) ] = []
my_dict[ (x,y) ].append( [a,b] )
但是,在 Perl 中,我必须对键和值使用 refs。所以我当然可以说:
$keyref = [ x1, y1 ]
$valref = [ a, b ]
%my_hash = { $keyref => $valref }
但是当另一对 (x2,y2) 出现时会发生什么?即使 x2==x1 和 y2==y1, $keyref=[x2,y2] 也会与之前生成的 keyref 不同,所以我看不到进行查找的方法。当然,我可以将 (x2,y2) 与每个取消引用的哈希键进行比较,但毕竟上帝给了我们哈希表,正是为了避免这样做的需要。
有 Perl 解决方案吗?
谢谢,
-W。
I have a problem where pairs of numbers map to other pairs of numbers. For instance, (1,2)->(12,97). Some pairs may map to multiple other pairs, so what I really need is the ability to map a pair into a list of lists, like (1,2)->((12,97),(4,1)). At the end of the day I want to process each of the values (i.e., each list of lists) separately.
In Python, I could do this by simply saying:
key = ( x, y )
val = [ a, b ]
if (x,y) not in my_dict:
my_dict[ (x,y) ] = []
my_dict[ (x,y) ].append( [a,b] )
However, in Perl, I have to use refs for the keys and values. So I can certainly say:
$keyref = [ x1, y1 ]
$valref = [ a, b ]
%my_hash = { $keyref => $valref }
But what happens when another pair (x2,y2) comes along? Even if x2==x1 and y2==y1, $keyref=[x2,y2] will differ from the previous keyref generated, so I do not see a way to do the lookup. Of course, I could compare (x2,y2) with each dereferenced hash key, but after all, God gave us hash tables precisely to avoid the need to do so.
Is there a Perl solution?
Thanks,
-W.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Perl 中,所有哈希键都是字符串,或者在查找之前“字符串化”。使用数组引用作为键通常是错误的方法。
使用“二维”哈希怎么样?
In Perl, all hash keys are strings, or are "stringified" before lookup. Using an array reference as a key is usually the wrong approach.
What about using a "two-dimensional" hash?
与 Perl 中的大多数内容一样,TMTOWTDI。
选项 1:使用多维数组模拟
另请参阅内置变量的文档 <代码>$;。
选项 2:使用 Hash::MultiKey 模块
选项 3:使用 HoH(哈希值的哈希值)
Like most things in Perl, TMTOWTDI.
Option 1: Use multidimensional array emulation
See also the documentation for the built-in variable
$;
.Option 2: Use the Hash::MultiKey module
Option 3: Use a HoH (hash of hashes) instead
我最终使用了 Socket Puppet 的解决方案(以 Michael Carmen 的选项 3 的形式)。仅供参考,这里有一个小 Perl 脚本,它执行我的应用程序中所需的所有操作。
打印的第 2:,3: 和 4:,5: 只是使用不同的语法来做同样的事情,而第 0: 和 1: 行只是为了一路进行健全性检查。
这添加到建议的解决方案中的是使用数组数组作为与键一起使用的值。
PS 我本想将其添加为评论,但它太长了,我认为包含一个有效的示例是有意义的。
I ended up using Socket Puppet's solution (in the form of Michael Carmen's Option 3). FYI, here is a little Perl script that carries out all the operations I need in my app.
Printed lines 2:,3: and 4:,5: just use different syntax to do the same thing, and lines 0: and 1: were just intended as sanity checks along the way.
What this this adds to the suggested solution is the use of an array of arrays as the value that goes along with a key.
P.S. I would have added this as a comment but it was too long, and I thought it made sense to include a worked example.