How to keep associative array order?

后端 未结 3 603
抹茶落季
抹茶落季 2020-12-05 02:32

I try to iterate over an associative array in Bash.

It seems to be simple, but the loop doesn\'t follow the initial order of the array.

Here is a simple scri

3条回答
  •  北海茫月
    2020-12-05 03:07

    My approach is to create a sorted array of keys first:

    keys=( $( echo ${!dict[@]} | tr ' ' $'\n' | sort ) )
    for k in ${keys[@]}; do
        echo "$k=${dict[$k]}"
    done
    

提交回复
热议问题