Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

help with dereferencing variables



In the following code, method 1 gives the expected results.  Method 2, 
however, breaks with the error:  unknown file attribute.  Is the eval in 
Method 1 required, or is there a correct syntax to make the dereference occur 
in Method 2 without an eval?

(This is a code fragment of a routine that will assign array values to 
parameters.)

Thanks, scowles at earthlink dot net

#!/bin/zsh
# variable 1 initialize.
m1=
# variable 1 values.
m2="
	/ld1
	/ld2
"
# variable 2 initialize.
m3=
# variable 2 values.
m4="
	/pk1
	/pk2
"
# correspondence matrix
cm="
	m1 m2
	m3 m4
"
rows=${#${(f)cm}}
for (( row = 1 ; $row <= $rows ; row++ ))
do
	key=${${=${${(f)cm}[${row}]}}[1]}
	val=${${=${${(f)cm}[${row}]}}[2]}
	# method 1:
	b=$(echo "${key}=( ${(@)${(P)val}} )")
	eval $b
	echo "	>${key}<:  ${(P)${key}}"
	# method 2:
	${key}=( ${(@)${(P)${val}}} )
	echo "	>${key}<:  ${(P)${key}}"
done
exit 0




Messages sorted by: Reverse Date, Date, Thread, Author