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

Re: How to iterate over an array of associative arrays



* Peter Stephenson (Sun, 14 Jul 2013 18:21:17 +0100)
> On Sun, 14 Jul 2013 15:36:03 +0200
> Thorsten Kampe <thorsten@xxxxxxxxxxxxxxxx> wrote:
> > Yes, I really wanted to pass the actual associative arrays, but if I can 
> > pass the names that's fine with me, too.
> 
> So maybe you're looking for:
> 
> typeset -A AssocArr1 AssocArr2
> typeset -a array
> typeset key value
> 
> AssocArr1=(key1 value1)
> AssocArr2=(key2 value2)
> 
> # array is a normal array containing key/value pairs
> array=(${(kv)AssocArr1} ${(kv)AssocArr2})
> 
> # scan array by pairs
> for key value in $array
>     do echo $key $value
> done

Looks good but as far as I can see it does not allow me to iterate over 
assiociative arrays so I can get the value of a certain key from all 
arrays.

Say I have...

AssocArr1=(key1 value11
           key2 value12)

AssocArr2=(key1 value21
           key2 value22)

So how do I iterate over...

    array=(AssocArr1 AssocArr2)

...so that I get value12 (= AssocArr1[key2]) in the first iteration and 
value22 (= AssocArr2[key2]) in the second?

Thorsten



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