Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: easy calling of associative array?
- X-seq: zsh-users 20868
- From: ZyX <kp-pav@xxxxxxxxx>
- To: Ray Andrews <rayandrews@xxxxxxxxxxx>, Zsh Users <zsh-users@xxxxxxx>
- Subject: Re: easy calling of associative array?
- Date: Mon, 02 Nov 2015 02:55:50 +0300
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex.ru; s=mail; t=1446422151; bh=dx0DfoFI3dOFCPamJp01kUOlNZaUrGJ87DpojWI+xuM=; h=From:To:In-Reply-To:References:Subject:Date; b=mW1HVX5PjFLRmERSxFxvTcLzdH1fnfeFTA9IEU4Gl7e4uCSSMD3F0gF2XFt1HVOoW Nj431zkl+YqidHPKMn2BBitCGqyk9rabeuOAF+sS5IUAjAH1XfCA8uBopyPSPPvDbg goM6tzuZHbt2P7PbEldQaAzUV8Utzr06Kg5WaKT0=
- In-reply-to: <56369C7B.2030604@eastlink.ca>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <56369C7B.2030604@eastlink.ca>
02.11.2015, 02:44, "Ray Andrews" <rayandrews@xxxxxxxxxxx>:
> test ()
> {
> eval "baz=\$${1}[key1]"
> echo $baz
> eval "foo=\$${1}[key2]"
> echo $foo
> ...
> }
>
> If I want to pass the name of an associative array to a function via
> "$1" and then work with it's keys, the above succeeds but it's sure
> laborious. Is there some way to do the eval just once? So that I can
> end up doing:
>
> if [ "xyz[key1]" = '1' ]; then ...
>
> ... as I experiment, there seems to be no way of replacing 'xyz' with
> any expression that will do the job without needing eval's help every
> time, but I'll bet there's a way.
There is ${(P)} for indirect referencing:
% foo=path
% echo ${${(P)foo}[1]} $path[1]
/home/zyx/.local/bin /home/zyx/.local/bin
. This is in `man zshexpn`.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author