数组与 Perl 有什么不同?
我可以在 Perl 中创建二维数组。二维数组与哈希有什么不同?当我们可以使用数组模拟哈希功能时,为什么哈希是 Perl 中的原始数据结构?
I can create 2-d arrays in Perl. How different is 2d arrays from Hash? Why is hash a primitive data structure in perl, when we can simulate the hash features using arrays?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许您的意思是您可以拥有这样的数据结构:
对于小数据,这效果很好,但请考虑这样的数组具有大约 1000 个条目的情况。然后,要通过键查找特定条目,您必须搜索整个数组,这很慢。
原始哈希类型允许快速查找,这正是它的用途。
Perl 中内置哈希表的另一个原因是它们是有用的数据结构,因此并非每个程序员都需要实现自己的哈希表。
Probably you mean you can have a data structure like this:
For small data this works well, but consider the case that such an array has about 1000 entries. Then, to find a specific entry by its key, you have to search through the whole array, which is slow.
The primitive hash type allows for quick lookups, and this is exactly what it is used for.
Another reason that hash tables are built into Perl is that they are useful data structures, so not every programmer should need to implement his own.
在构建许多类型的对象时,new 方法可以传递一个可能显示为散列的参数列表,但它实际上只是一个数组,其中每个偶数都是一个键,每个奇数是一个值。
这相当于:
但是,您可以指示 new() 查找哈希,这看起来更像是:
如果这不能回答您的问题,我希望我至少提供了一些见解。
In construction of many types of objects, the
new
method can be passed a list of arguments that may appear as a hash, but it really is just an array, where every even number is a key, and every odd number is a value.This is the equivalent to:
You can, however, instruct new() to look for a hash, which would appear more like:
If this doesn't answer your question, I hope I at least provided some insight.