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

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -