关联数组的键$ {!a [@]}和值$ {a [@]}是否以相同顺序扩展? [英] Do keys ${!a[@]} and values ${a[@]} of associative arrays expand in the same order?

查看:70
本文介绍了关联数组的键$ {!a [@]}和值$ {a [@]}是否以相同顺序扩展?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在bash中,关联数组(也称为字典或哈希图)是无序的.对于关联数组 a ,我们可以使用 $ {!a [@]} 列出所有键(也称为索引),并使用 $ {a [@]} .我知道这些结构不会按固定顺序扩展.我想知道是否至少有 some 个保证.我找不到.但是,在任何实现中 ["$ {a [*]}" = $ {a [*]}] 似乎都是不现实的.同样,似乎 $ {!a [@]} 的扩展顺序与 $ {a [@]} 相同.如果我们在 $ {!a [@]} 中位置 n 处找到键 x ,则将找到值 $ {a [x]} 也位于 $ {a [@]} 中的位置 n .当然,我们假设在 $ {!a [@]} $ {a [@]} 的扩展之间未修改 a

In bash, associative arrays (also known as dictionaries or hash maps) are unordered. For the associative array a we can list all keys (also known as indices) with ${!a[@]} and all values with ${a[@]}. I know that these constructs do not expand in a fixed order. I wondered if there are at least some guarantees. I couldn't find any. However, it seems unrealistic that [ "${a[*]}" = ${a[*]} ] will fail in any implementation. Likewise, it seems that ${!a[@]} expands in the same order as ${a[@]}. If we find key x at position n in ${!a[@]} then we will find value ${a[x]} at position n in ${a[@]} too. Of course we assume that a is not modified between the expansions of ${!a[@]} and ${a[@]}.

declare -A a=([x]=1 [y]=2 [z]=3)
printf %s\\n "${!a[*]}" "${a[*]}"

# As output I would expect one of the following blocks ...
# (corresponding keys and values are in the same column)
x y z    x z y    y x z    y z x    z x y    z y x
1 2 3    1 3 2    2 1 3    2 3 1    3 1 2    3 2 1

# ... but never something like ...
# (at least one key doesn't share a column with its value)
x y z    x y z    y x z
1 3 2    2 3 1    2 3 1    ...

问题

  • 对于任何现有的bash版本和关联数组 a ,可以 $ {!a [@]} $ {a [@]} 以这样的方式扩展:键及其对应的值具有不同的顺序?换句话说:
  • Question

    • For any existing bash version and associative array a, could ${!a[@]} and ${a[@]} expand in such a way that keys and and their corresponding values have a different order? In other words:
    • # Are there values for a and i
      # such that this script could print "different order"
      declare -A a=(...)
      declare -i i=...
      keys=("${!a[@]}")
      values=("${a[@]}")
      [ "${a[keys[i]]}" != "${values[i]}" ] && echo "different order"
      

      奖金问题

      • $ {!a [*]} $ {a [@]} 在bash的手册中还是其他一些官方文件?
      • 我们可以从实施本身中获得更多/其他保证吗??这些保证中的某些保证是否随bash版本的不同而改变?这些保证中的某些保证在即将到来的bash版本中可能会改变吗?
      • Bonus Questions

        • Are there any guarantees on the expansion orders of ${!a[*]} and ${a[@]} in bash's manual or some other official document?
        • Can we assume further/other guarantees from the implementation itself? Did some of these guarantees change with different versions of bash? Are some of these guarantees likely to change in upcoming versions of bash?
        • 推荐答案

          在bash联机帮助页中我没有看到任何有关关联数组的键排序的信息(我使用的是4.3.48).看到bash 5.0的源代码( hashlib.c )还表明bash使用了最简单的基于XOR哈希的算法,没有任何随机化,因此不应该在进程或机器之间将顺序随机化,这与其他方式不同您可以在Perl中找到更复杂的算法.

          I'm not seeing any mention of associative array's key ordering in the bash manpage (I'm using 4.3.48). Seeing the source code of bash 5.0 (hashlib.c) also shows that bash uses the simplest hashing XOR-based algorithm without any randomization, so the order should not be randomized between processes or machines, unlike some other more sophisticated algorithms you'd find in, say, Perl.

          话虽如此,但依赖于特定的键顺序或恒定的顺序仍然是不明智的,因为Perl-land的经验告诉我们.而且,由于bash联机帮助页中没有说明订购,因此无法保证.哈希实现可以随时替换或重写.

          That being said, it's still unwise to depend on particular key ordering or on the order being constant, as the experience in Perl-land taught us. Moreover, since the bash manpage doesn't say about ordering, there's no guarantee. The hash implementation could be replaced or rewritten anytime.

          这篇关于关联数组的键$ {!a [@]}和值$ {a [@]}是否以相同顺序扩展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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