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

Re: How to iterate over an array of associative arrays



* Mikael Magnusson (Sun, 14 Jul 2013 15:10:22 +0200)
> 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?

Yes, I really wanted to pass the actual associative arrays, but if I can 
pass the names that's fine with me, too.

> 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

Doesn't work for me:

"""
array=(AssocArr1 AssocArr2)

for elem in $array
    do echo ${${(P)elem}[key]}
done
"""

[empty output]

Thorsten



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