元素在hash,perl中的顺序.我没有按我在哈希中定义的顺序获取值 [英] order of elements in hash , perl . i am not getting the values in order in which i defined in hash

查看:165
本文介绍了元素在hash,perl中的顺序.我没有按我在哈希中定义的顺序获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

在打印散列时,我没有按顺序获得值,在散列中定义了这些值.
示例:

Hello all,

While printing a hash , I am not getting the values in order , in which I defined in a hash.
Example:

my $hash=(
         ''bangalore''=>''20'',
         ''Mumbai''=>''23'',
         ''pune''=>''45'',
         ''aligarh''=>''56''
         );


因此,如果我打印此哈希,则无法保证它将按照我定义的顺序打印:mad :,我的意思是,在打印哈希时,mnmnai可能会先出现,然后是班加罗尔,所以有人可以帮助我理解我为什么这么做没有按我在哈希中定义的顺序获取值.

*上面的哈希只是一个例子
我的actulay散列包含一些我需要一个命令运行的命令(执行该命令时,顺序非常重要).
我知道,在这种情况下,我可以使用数组:)
但是可以有人解释哈希如何将数据存储在内存中,如果我想按哈希中定义的顺序打印值,该怎么做?

:confused:


so if I print this hash, there is no guarantee that it will print in the same order as i defined :mad:, I mean while printing the hash mumnai may come first and then bangalore , so can someone help me in understanding why am I not getting the values in order as I defined in hash.

* above hash is just an example
my actulay hash contains some command which I need to run one by one (order is very important while executing the command).
I know, I can use array in this case :)
BUT can some one explain how hash stores data in memory , and if I want to print the values in order as I defined in hash , how to do that?

:confused:

推荐答案

hash =( ''bangalore''=>''20'', ``孟买''=>''23'', ''pune''=>''45'', ''aligarh''=>''56'' );
hash=( ''bangalore''=>''20'', ''Mumbai''=>''23'', ''pune''=>''45'', ''aligarh''=>''56'' );


因此,如果我打印此哈希,则无法保证它将按照我定义的顺序打印:mad :,我的意思是,在打印哈希时,mnmnai可能会先出现,然后是班加罗尔,所以有人可以帮助我理解我为什么这么做没有按我在哈希中定义的顺序获取值.

*上面的哈希只是一个例子
我的actulay散列包含一些我需要一个命令运行的命令(执行该命令时,顺序非常重要).
我知道,在这种情况下,我可以使用数组:)
但是可以有人解释哈希如何将数据存储在内存中,如果我想按哈希中定义的顺序打印值,该怎么做?

:confused:


so if I print this hash, there is no guarantee that it will print in the same order as i defined :mad:, I mean while printing the hash mumnai may come first and then bangalore , so can someone help me in understanding why am I not getting the values in order as I defined in hash.

* above hash is just an example
my actulay hash contains some command which I need to run one by one (order is very important while executing the command).
I know, I can use array in this case :)
BUT can some one explain how hash stores data in memory , and if I want to print the values in order as I defined in hash , how to do that?

:confused:


perl中的哈希称为哈希,因为它们将数据存储在哈希表中.哈希表使用哈希函数根据您提供的值计算密钥,并允许快速搜索,但会占用内存.

在您的示例中,假设哈希表有26个条目,并且哈希函数使用第一个字母(不区分大小写)来确定将每个项目放在表中的位置.因此,哈希表将类似于:

Hashes in perl are called hashes because they store the data in a hash table. Hash tables use a hash function to calculate a key based on the value you give it and allow for fast searches at the expense of memory.

Using your example, say the hash table has 26 entries, and the hash function uses the first letter (case insensitive) to determine where to put each item in the table. So the hash table would look something like:

 0: ''aligarh''=>''56''
 1: ''bangalore''=>''20''
 2:
 3:
...
12: ''Mumbai''=>''23''
...
15: ''pune''=>''45''
...
25:



插入顺序不会影响哈希表中的顺序,否则以后将无法在不知道何时插入值的情况下查找该值.因此,无论您将项目插入哈希的顺序如何,哈希表都将处于相同状态.在实际使用中,散列函数更复杂以减少冲突(将两个键放在同一插槽中).

因此,要回答您的问题如果我想按哈希中定义的顺序打印值,该怎么做?",答案是您不能不使用哈希(数组,或其他一些跟踪插入顺序的变量,或者如果您确实在寻找一种有趣的方式来读取它,则可以通过读取您的perl文件并对其进行解析.)

有关更长时间且可能更好的解释,请维基百科 [



The insertion order will not affect the order in the hash table, otherwise there would be no way to look up a value later without knowing when it was inserted. Thus, no matter what order you insert items into the hash, the hash table will end up in the same state. In real-world uses, the hash function is more complicated to reduce collisions (two keys being put in the same slot).

So, to answer your question "if I want to print the values in order as I defined in hash, how to do that?", the answer is that you can''t without using something besides just the hash (either an array, or some other variable keeping track of insertion order, or maybe by reading in your perl file and parsing it if you''re really looking for a fun way to do it.)

For a much longer, and likely better explanation, wikipedia[^] is always a place to start.


这篇关于元素在hash,perl中的顺序.我没有按我在哈希中定义的顺序获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆