密钥和散列值将具有相同的“顺序”在Perl中? [英] Will keys and values of a hash have the same "order" in Perl?
问题描述
我知道一个散列不是在perl中排序的。我所关心的是如果我可以依靠与索引关系产生的键和值。
I understand that a hash is not ordered in perl. What I am concerned about is if I can depend on the keys and values coming out with an index relationship.
假设我有这个散列值
my %h = ("a" => 1, "b" => 2, "c" => 3, "d" => 4);
如果我做了键%h
,我可能会得到
If I do keys %h
, I might get
("b", "a", "d", "c")
我会保证值%h
会以相同的顺序出现匹配键?我可以期待吗?
Would I be guaranteed that values %h
would come out in the same order to match keys? Can I expect?
(2, 1, 4, 3)
还是不能保证键%h
和之间有任何索引关系。值%h
?
推荐答案
是的。只要散列没有改变(插入或删除),键
,值
和每一个
将保持相同的顺序:
Yes. As long as the hash is not changed (insertion or deletion), keys
, values
and each
will keep the same order:
只要给定的哈希值未经修改,您可以依赖键,值和每个重复返回相同的顺序。
So long as a given hash is unmodified you may rely on keys, values and each to repeatedly return the same order as each other.
- 从 perldoc -f keys
– from perldoc -f keys
所以你可以安全地复制一个哈希像:
So you could safely copy a hash like:
my %orig = ...;
my %copy;
@copy{keys %orig} = values %orig;
这篇关于密钥和散列值将具有相同的“顺序”在Perl中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!