Ordered associative arrays in bash -


i can following in bash:

declare -a data data[a]="aaa" data[c]="ccc" data[b]="bbb"  in "${!data[@]}" ;     printf "%-20s ---> %s\n" "$i" "${data[$i]}" done 

which outputs:

a                    ---> aaa b                    ---> bbb c                    ---> ccc 

that is, associative array reordered (i assume using lexicographic ordering on keys, not sure), , loses original order in created data. wanted instead:

a                    ---> aaa c                    ---> ccc b                    ---> bbb 

in python use ordereddict instead of plain dict that. there similar concept in bash?

as answered, associative arrays not ordered. if want ordering in array, use below work-around.

declare -a data data_indices=() data[a]="aaa"; data_indices+=(a) data[c]="ccc"; data_indices+=(c) data[b]="bbb"; data_indices+=(b)  in "${data_indices[@]}" ;     printf "%-20s ---> %s\n" "$i" "${data[$i]}" done 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -