Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #15322 > unrolled thread

Associative array entries order differs from declaration

Started byLéa Gris <lea.gris@noiraude.net>
First post2019-08-15 17:26 +0200
Last post2019-08-15 17:26 +0200
Articles 1 — 1 participant

Back to article view | Back to gnu.bash.bug

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Associative array entries order differs from declaration Léa Gris <lea.gris@noiraude.net> - 2019-08-15 17:26 +0200

#15322 — Associative array entries order differs from declaration

FromLéa Gris <lea.gris@noiraude.net>
Date2019-08-15 17:26 +0200
SubjectAssociative array entries order differs from declaration
Message-ID<mailman.167.1565882784.30381.bug-bash@gnu.org>

[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

[toc] | [standalone]


Back to top | Article view | gnu.bash.bug


csiph-web