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

Re: help with dereferencing variables

On Feb 14,  9:59am, S. Cowles wrote:
} 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?

The short answers are "not precisely" and "no".

} 	# method 1:
} 	b=$(echo "${key}=( ${(@)${(P)val}} )")
} 	eval $b

You don't need the $(echo) and the assignment to $b here.  It should be
enough to do

	eval ${key}'=( ${(P)val} )'

(note placement of single quotes).

} 	# method 2:
} 	${key}=( ${(@)${(P)${val}}} )

This syntax is not an assignment, because the stuff to the left of the
equals sign is not an identifier name.  (This used to work in older
versions of zsh because the variable was expanded before zsh looked
for assignment syntax, but that was incompatible with other shells.)

Even if this were an assignment, The ${(@)...} is extraneous when the
whole thing is not in double quotes.  And you don't need ${(P)${val}},
just ${(P)val} is enough.

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