Add ordering to associative array using an indexed array with matching values.
This commit is contained in:
@@ -25,3 +25,17 @@ done
|
||||
# key: repo_two
|
||||
# repo_one is true
|
||||
# key: repo_three
|
||||
|
||||
# NOTE: Be care of -A versus -a in the array declaration. A -a makes an *indexed* array as opposed to an *associative* array.
|
||||
# But given that an associative array is actually a hash table, it doesn't store the pairs in the order you put them
|
||||
# into the array, they are instead stored in their hash value order. If order matters you need to use an indexed array.
|
||||
# The following shows how to use both an indexed array and an associative array together to get both, if that's what you need.
|
||||
# (Ref https://stackoverflow.com/questions/29161323/how-to-keep-associative-array-order)
|
||||
declare -a arrOrder ; declare -A arrData
|
||||
arrOrder+=( repo_one ) ; arrData[repo_one]=true
|
||||
arrOrder+=( repo_two ) ; arrData[repo_two]=true
|
||||
arrOrder+=( repo_three ) ; arrData[repo_three]=false
|
||||
for i in "${arrOrder[@]}"
|
||||
do
|
||||
echo "$i: ${arrData[$i]}"
|
||||
done
|
||||
|
||||
Reference in New Issue
Block a user