Hello StackExchange pros!
I am working on a zsh project for macOS. I used typeset to create three associative arrays to hold values, and a fourth array to reference the individual arrays. Is it possible to iterate over the arrCollection to retrieve the key/value pairs from each of the member arrays? Note that the keys in the arrays below are not the same as my production script--they are simply key indices rather than the more descriptive keys you might find in an associative array.
I thought I could use parameter expansion like this:
for k in $(sort <<< "${(kvF)arrCollection}"); do
echo "$arrCollection["${(kvF)k}"]"
done
I don't have it quite right though. Can anyone help? Expected output will be a list of all items from all three arrays separated by a newline.
Full script sample below. Usage: arrTest.sh showAll
#!/bin/zsh
key=$1
typeset -A arrOne arrTwo arrThree
typeset -A arrCollection
#Plan is to use an array of arrays so that a for loop can be used later to loop
#through each key/value pair looking for a value that matches some pattern in an if statement
#(if statement not included here). In the showAll case, how can I use parameter expansion to print all
#of the values in each array? The if statement will further constrict what is actually echoed based on its
#value.
arrOne[1]="First"
arrOne[2]="Second"
arrOne[3]="Third"
arrOne[4]="Fourth"
arrTwo[1]="Purple"
arrTwo[2]="Orange"
arrTwo[3]="Red"
arrTwo[4]="Green"
arrTwo[5]="Blue"
arrThree[1]="First"
arrThree[2]="Red"
arrThree[3]="Planet"
arrThree[4]="Sun"
arrThree[5]="Moon"
arrThree[6]="Star"
#Array of arrays
arrCollection[1]=arrOne
arrCollection[2]=arrTwo
arrCollection[3]=arrThree
#Expect a parameter
if [ -z "$key" ]
then
echo "Please enter a parameter"
else
case "$key" in
showAll)
for k in $(sort <<< "${(kvF)arrCollection}"); do
#This is the part I am having trouble with
echo "$arrCollection["${(kvF)k}"]"
done
exit 1
;;
*)
echo "Something goes here"
exit 1
;;
esac
fi