#!/usr/bin/env bash
# Declare and populate an associative array a
unset a
declare -A a=(
["one"]="first"
["two"]="second"
["three"]="third"
["four"]="last")
)
typeset -p a # show actual declaration order that differs from real one
# Show how the chaotic order affect iteration of the array
for v in "${a[@]}"; do
echo "$v"
doneOutput: declare -A a=([two]="second" [three]="third" [four]="last" [one]="first" second third last firstThis behavior looks just wrong and it is just same if you build the array incrementally:
unset a; declare -A a; a=(["one"]="first"); a+=(["two"]="second"); a+=(["three"]="third"); a+=(["four"]="last"); typeset -p a
Is there a way to control the order of entries in an associative array? What rules applies to the order of entries? -- Léa Gris
signature.asc
Description: OpenPGP digital signature
