Well - further thinking drew me to this solution, which probably could be applied to the problem behind the question: Loop through array x
!
> x=(a:1 b:2 c:3) > typeset -A z > for i in $x; do z+=(${(s,:,)i}); done > echo $z 1 2 3 > echo ${z[a]} 1
Hope more helpful than first answer (s) :-)
Since x
is an array, you should not forget [@]
, i.e. print ${(s,:,)x[@]}
; this way your code becomes print -l ${(s,:,)x[@]}
; but the solution remains another step:
> typeset -A z > x="a:1 b:2 c:3" > z=($(echo ${(s,:,)x})) > echo $z[a] 1
Assigning an associative array is done with the output of the echo statement.
To clarify and include your original example x
:
> typeset -A z > x=(a:1 b:2 c:3) > z=($(echo ${(s,:,)x[@]})) > echo ${z[a]} 1 > echo ${z[b]} 2
(edit: translated thoughts midwriting: -D should now make more sense) (edit 2: striked brainf * rt - the similarities between zsh and bash are not as great as I assumed)
source share