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

Re: How to iterate over an array of associative arrays



On 14 July 2013 14:35, Thorsten Kampe <thorsten@xxxxxxxxxxxxxxxx> wrote:
> Hi,
>
> I'd like to iterate over elements of an array. The elements are
> associative arrays. This is my code:
>
> """
> #! /usr/bin/env zsh
> emulate -R zsh
>
> setopt nounset
>
> declare -A elem AssocArr1 AssocArr2
>
> AssocArr1=(key value1)
> AssocArr2=(key value2)
>
> array=($AssocArr1 $AssocArr2)

Do you really want these $ here?

> for elem in $array
>     do echo $elem[key]
> done
> """
>
> ./test.zsh:14: elem[key]: parameter not set

You need to use the (P) flag to access a parameter indirectly.
% foo=(one two)
% bar=foo
% echo $bar[2]
o
% echo ${${bar}[2]}
o
% echo ${${(P)bar}[2]}
two

--
Mikael Magnusson



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