如何在 Perl 中迭代 Hash(of Hashes)?
我有哈希,其中键的值是其他哈希。
示例: {'key' =>; {'key2'=>; {'key3'=>; 'value'}}}
我如何迭代这个结构?
I have Hash where values of keys are other Hashes.
Example: {'key' => {'key2' => {'key3' => 'value'}}}
How can I iterate through this structure?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这个答案建立在 Dave Hinton 背后的想法之上——即编写一个通用子例程来遍历哈希结构。这样的哈希遍历器获取代码引用并简单地为哈希中的每个叶节点调用该代码。
通过这种方法,同一个哈希遍历器可以用来做很多事情,具体取决于我们给它的回调。为了获得更大的灵活性,您需要传递两个回调 - 一个在值是哈希引用时调用,另一个在值是普通标量值时调用。 Marc Jason Dominus 的优秀著作 Higher Order Perl 更深入地探讨了此类策略。
This answer builds on the idea behind Dave Hinton's -- namely, to write a general purpose subroutine to walk a hash structure. Such a hash walker takes a code reference and simply calls that code for each leaf node in the hash.
With such an approach, the same hash walker can be used to do many things, depending on which callback we give it. For even more flexibility, you would need to pass two callbacks -- one to invoke when the value is a hash reference and the other to invoke when it is an ordinary scalar value. Strategies like this are explored in greater depth in Marc Jason Dominus' excellent book, Higher Order Perl.
这是你想要的吗? (未经测试)
Is this what you want? (untested)
这篇文章可能会有用。
This post may be useful.
前面的答案展示了如何推出您自己的解决方案,最好至少执行一次,这样您就可以了解 perl 引用和数据结构如何工作的本质。您绝对应该阅读 perldoc perldsc 和 perldoc perlref 如果您还没有。
但是,您不需要编写自己的解决方案 - CPAN 上已经有一个模块,它将为您迭代任意复杂的数据结构:数据::访客。
The earlier answers show how to roll your own solution, which is good to do at least once so you understand the guts of how perl references and data structures work. You should definitely take a read through perldoc perldsc and perldoc perlref if you haven't already.
However, you don't need to write your own solution -- there is already a module on CPAN which will iterate through arbitrarily-complex data structures for you: Data::Visitor.
这并不是一个真正的新答案,但我想分享如何做更多的事情
只需递归打印所有哈希值,但如果需要也可以修改它们。
这是我对 dave4420 答案的轻微修改,其中
该值作为参考传递给回调,所以我的回调
然后例程可以修改哈希中的每个值。
我还必须重建哈希,因为每个循环都会创建副本
不是参考文献。
This is not really a new answer but I wanted to share how to do more than
just print all the hash values recursively but also to modify them if needed.
Here is my ever so slight modification of the dave4420's answer in which
the value is passed to the callback as a reference so my callback
routine could then modify every value in the hash.
I also had to rebuild the hash as the while each loop creates copies
not references.
如果您使用 perl 作为“CPAN 解释器”,那么除了
Data::Visitor< /code>
和
Data::Deep
有一个超级简单的Data::Traverse
:输出:
$a
和$b
在此处被视为特殊变量(与sort()
一样),而在<代码>traverse()函数。Data::Traverse
是一个非常简单但非常有用的模块,没有非核心依赖项。If you are using perl as a "CPAN interpreter" then in addition to
Data::Visitor
andData::Deep
there is the super simpleData::Traverse
:Output:
$a
and$b
are treated as special variables here (as withsort()
) while inside thetraverse()
function.Data::Traverse
is a very simple but immensely useful module with no non-CORE dependencies.你必须循环它两次。 IE
You will have to loop through it twice. i.e.