[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Associative array entries order differs from declaration

From: Léa Gris
Subject: Associative array entries order differs from declaration
Date: Thu, 15 Aug 2019 17:26:03 +0200
User-agent: Telnet/1.0 [tlh] (PDP11/DEC)

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=(
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"

declare -A a=([two]="second" [three]="third" [four]="last" [one]="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

Attachment: signature.asc
Description: OpenPGP digital signature

reply via email to

[Prev in Thread] Current Thread [Next in Thread]