#!/bin/bash
Lets say you have an array with both integer indexes and string indexes like so...
Edit... Note: This block will be in a loop in my app
declare -A arr
arr[${#arr[@]}]=someVal1
arr[${#arr[@]}]=someVal2
arr[someBool]=true
arr[someOtherString]="Some Text"
arr[${#arr[@]}]=someVal3
Now lets say I want to compare the number of array items for some reason...
[ "${#arr[@]}" = 5 ] && echo "there are 5 array items";
output...
there are 5 array items
Ok thats all and good but lets take a look at our array
declare -p arr
exit
output...
declare -A arr='([someBool]="true" [someOtherString]="Some Text"
[0]="someVal1" [1]="someVal2" [4]="someVal3" )'
Notice that the integer indexes go 0,1,4
So my Question is what should replace
arr[${#arr[@]}]=
so that the integer array indexes work out properly? (0,1,2)
Edit... Temp Solution Based on Henk Langeveld's input... Temp because it should be able to edit an array passed to it not just edit a global array statically.
#!/bin/bash
getArrIntIndexNum(){
local x=0
for i in "${!arr[@]}"; do
[[ $i =~ ^-?[0-9]+$ ]] && x=$((x+1))
done
echo $x
}
declare -A arr
arr[$(getArrIntIndexNum)]=someVal1
arr[$(getArrIntIndexNum)]=someVal2
arr[someBool]=true
arr[someOtherString]="Some Text"
arr[$(getArrIntIndexNum)]=someVal3
declare -p arr
output...
declare -A arr='([someBool]="true" [someOtherString]="Some Text" [0]="someVal1" [1]="someVal2" [2]="someVal3" )'
declare -poutput, as if there's a right margin. The result can still be interpreted by bash.