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 10318
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: zsh-users@xxxxxxxxxx
- Subject: Re: Keying arrays to names: is there an array of arrays?
- Date: Sat, 27 May 2006 15:10:44 -0700
- In-reply-to: <m3wtc7gu80.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>
On May 27, 7:09pm, Johann 'Myrkraverk' Oskarsson wrote:
}
} > This reminds me I was going to post a general solution along the same
} > lines which deals with embeded spaces etc.:
}
} Is it possible to abstract that trick into functions? That is, so the
} function takes the array name, key, and value as arguments?
I'll assume you mean the *associative* array (hash) name.
A function to do the assignment isn't too terrible, though there are
scoping issues -- either the hash already has to exist, or you can
only create it in the global scope by using typeset -g:
setbykey() {
local h=$1 k=$2
shift 2
set -- ${(q)*}
typeset -gA $h
typeset -g $h\[$k\]="$*"
}
A function to read back the value is a bit trickier. What do you want
to do with the array once you have it? A common fallback would be to
stuff it in $reply:
getbykey() {
set -- $1\[$2\]
reply=( ${(Q)${(z)${(P)1}}} )
}
typeset -A hash
setbykey hash key one 'two three' '"four five six"'
getbykey hash key
print -rl $reply
Incidentally:
} I classify Outlook mail as spam, use something else.
Someone asking for assistance has no right to dictate terms. You want
my help, you take it the way I send it. I don't happen to use Outlook,
but next time I see something like this, I just won't answer.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author