Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Keying arrays to names: is there an array of arrays?
- X-seq: zsh-users 10327
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: ZSH Users <zsh-users@xxxxxxxxxx>
- Subject: Re: Keying arrays to names: is there an array of arrays?
- Date: Sun, 28 May 2006 11:17:24 -0700
- In-reply-to: <m3ejyeg9nh.fsf@xxxxxxxxxxxxxxxxxx>
- Mailing-list: contact zsh-users-help@xxxxxxxxxx; run by ezmlm
- References: <m33bewj29n.fsf@xxxxxxxxxxxxxxxxxx> <44771E3E.9070102@xxxxxxxxxxxxxxxxxx> <200605261540.k4QFeReG007573@xxxxxxxxxxxxxx> <m3wtc7gu80.fsf@xxxxxxxxxxxxxxxxxx> <060527151044.ZM25448@xxxxxxxxxxxxxxxxxxxxxx> <m3ejyeg9nh.fsf@xxxxxxxxxxxxxxxxxx>
On May 28, 2:34am, Johann 'Myrkraverk' Oskarsson wrote:
>
> The following version also works with IFS="" (I guess it will work
> with IFS equal to something sensible too).
>
> mapassign() {
> local h=$1 k=$2
> shift 2
> set -- ${(j: :q)*}
> typeset -gA $h
> typeset -g $h\[$k\]="$*"
> }
A better approach might be:
mapassign() {
emulate -L zsh
local h=$1 k=$2 IFS=$' \t\n\0'
shift 2
set -- ${(q)*}
typeset -gA $h
typeset -g $h\[$k\]="$*"
}
> Same with this one:
>
> mapread() {
> set -- $1\[$2\]
> reply=( ${(s: :Q)${(z)${(P)1}}} )
> }
That's actually incorrect. The (z) option is already splitting the
value; you shouldn't need or want to split it again with (s: :). If
the setting of IFS is preventing (z) from working properly, that's
probably a bug, but in any case the workaround is to make IFS local.
mapread() {
emulate -L zsh
local IFS=$' \t\n\0'
set -- $1\[$2\]
reply=( ${(Q)${(z)${(P)1}}} )
}
If you're going to use these functions for hash access anyway, then
you might as well also use ${(q)2} instead of $2 in both of them, to
avoid issues with non-alphanumeric keys.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author