Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Implement a key-value deserialization or something similar
- X-seq: zsh-users 21847
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: Zsh Users <zsh-users@xxxxxxx>
- Subject: Re: Implement a key-value deserialization or something similar
- Date: Wed, 7 Sep 2016 01:05:25 -0700
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brasslantern-com.20150623.gappssmtp.com; s=20150623; h=from:message-id:date:in-reply-to:comments:references:to:subject :mime-version; bh=qZayMkIJ2haaqizTFCUiT97ugLlxiLxblK/2XLFLeZM=; b=Xj4WpfA6gWi1N3G43Y/M7EUKBec6aOMqdKva//zZ5sFH65w1VDHKqWfWZ/plMRBZYm POR3hbFK10UF/DGaD6bw5m/hhmLBH1zTrrBTLPFR9LV5LYvtWCil1ZnCl8nlKVdmgbJn fqa6JvreCLVxdeBRq3kJUHMy+KOBRFNodL2IIF3MxYui0VEhg0ZQeEAqjbVBvzxd0b07 umd3mEYW8NitptP7zpNhwVIA9TBHpJiKyxWbmX9nCV//tGeXwrxr8pRWrtmj/KZb3X2b zn9ILtN+i7Iqaahs3jx8CBNbQFrNFLMYfqdCug6aZHecKbeC5SDRF5Vey9VDLNljDv7x 9/3Q==
- In-reply-to: <20160906002016.GB30336@fujitsu.shahaf.local2>
- 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: <CAKc7PVB19jOGwcsu5bL2zGuEioJXM9LuT009WDzZdg2eNobmQA__43171.2364581928$1473108546$gmane$org@mail.gmail.com> <20160906002016.GB30336@fujitsu.shahaf.local2>
On Sep 6, 12:20am, Daniel Shahaf wrote:
} Subject: Re: Implement a key-value deserialization or something similar
}
} Like this:
}
} % local -A reply=(mydata 1 otherdata true)
} % () { echo ${${(P)7}[otherdata]} } {1..6} reply
} true
}
} Or you could adopt the Perl convention:
}
} () {
} local -A args=( "$argv[@]" )
} } mydata 1 otherdata true
To more explicitly state what I think what Daniel is means with his
second example, the notion is that you can string key-value pairs
along in the argument list as ordinary arguments as long as you know
how many of the leading arguments are meaningful. E.g. if your
function takes four arguments, you can extend it like this:
FnOf4() {
local -A rest=( $argv[5,-1] )
argv[5,-1]=()
print -l "The positional args:" $*
if [[ -n $rest[foo] ]]; then
print -l "The foo arg:" $rest[foo]
fi
}
% FnOf4 these are positional and foo bar become rest
The positional args:
these
are
positional
and
The foo arg:
bar
If you don't know how many arguments will precede the "rest" pairs,
use a flag value such as "--" (that must not appear in the "rest"):
FnOfN() {
integer mark=$argv[(I)--]
local -A rest=( $argv[mark+1,-1] )
argv[mark,-1]=()
print -l "The positional args:" $*
if [[ -n $rest[foo] ]]; then
print -l "The foo arg:" $rest[foo]
fi
}
% FnOfN a batch of positional args and -- foo bar become rest
Daniel's first example amounts to setting this up in the caller and then
passing the name of the "rest" hash as $argv[-1], rather than passing
all the key-value pairs as positionals.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author