Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > gnu.bash.bug > #15322
| From | Léa Gris <lea.gris@noiraude.net> |
|---|---|
| Newsgroups | gnu.bash.bug |
| Subject | Associative array entries order differs from declaration |
| Date | 2019-08-15 17:26 +0200 |
| Message-ID | <mailman.167.1565882784.30381.bug-bash@gnu.org> (permalink) |
| References | <71660c94-58cc-c87f-9e5f-c0aa32013097@noiraude.net> |
[Multipart message — attachments visible in raw view] - view raw
While dealing with getting keys of arrays, I found out that associative
array keys where not registered in the same order as declared:
#!/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"
done
Output:
declare -A a=([two]="second" [three]="third" [four]="last" [one]="first"
second
third
last
first
This 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
Back to gnu.bash.bug | Previous | Next | Find similar | Unroll thread
Associative array entries order differs from declaration Léa Gris <lea.gris@noiraude.net> - 2019-08-15 17:26 +0200
csiph-web