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

Re: easy calling of associative array?



On Nov 1,  4:49pm, Ray Andrews wrote:
} Subject: Re: easy calling of associative array?
}
} On 11/01/2015 03:55 PM, ZyX wrote:
} >
} > There is ${(P)} for indirect referencing:
}
} I can't get it to work with associative arrays tho.

Yeah, it's a bit weird with associative arrays because everything is
passed by value.  ${(P)ref} is ${(vP)ref} and thus ${${(P)ref}[idx]}
doesn't get what you want, and ${(P)ref[idx]} is ${(P)${ref[idx]}}
which also isn't what you want.

So you have to get a little arcane:  ${(P)${:-${ref}[idx]}}.

- ${ref} is the name of the assocative array.

- ${ref}[idx] is therefore a string that has the same format as the
  array lookup you want to perform.

- ${:-${ref}[idx]} substitutes that string into a parameter expansion.

- ${(P)${:-${ref}[idx]}} then treats that substitution as another
  parameter expansion, and you end up where you wanted to be.

This is admittedly not ideal, but until we embark on a significant
rewrite of parameter expansion, we're stuck maneuvering values into
the right places at the right times.



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