Answer the question
In order to leave comments, you need to log in
What is the order of elements in a BASH associative array?
There is a script
declare -A AA
AA=()
for idx in $(seq 1 10)
do num=$(bc <<<"10^$idx")
AA+=( [$num]=$idx )
done
echo ${AA[@]}
# => 4 1 5 6 3 9 7 8 2 10
echo ${!AA[@]}
# => 10000 10 100000 1000000 1000 1000000000 10000000 100000000 100 10000000000
Answer the question
In order to leave comments, you need to log in
Hash tables do not have an insertion order, and although the apparent order is not random, you should never rely on it during development. The reason why the order is the same on different computers is because the hash function is the same.
For example, there is a hash function foo()
that returns a hash as a number. The results of this function may be as follows:
foo('1') = 123
foo('2') = 321
foo('3') = 12
foo('4') = 500
...
{12 => c, 123 => a, 321 => b, 500 => d}
. 3 1 2 4
, i.e. sorted by hash value. A regular hash table does not preserve the insertion order of elements.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question