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

Re: easy calling of associative array?



On 11/02/2015 08:28 AM, Bart Schaefer wrote:
}     #$3=${(P)${:-${1}[$2]}}     #nothing works

Pardon, that was an obvious mistake.  Too much thrashing around with 'eval'.
Same assignment-syntax problem.

     set_v () {
       typeset -g "${1}[$2]=$3"	# quotes so [ ] isn't globbing
     }

Here you don't need the (P) indirection because ${1} and $2 are both
expanded before being passed to typeset, so you already extracted the
name that was passed in $1.

... and that's nicely readable too--the sort of syntax that seems intuitive, as I
mentioned.  I won't say this is all clear in my mind, but it's
at least tractable. One further question: I've never seen 'typeset' used to refer to anything but a simple variable. I note that the above does not work without
the 'typeset -g' so what does that accomplish?   I've never thought of the
positional parameters and being typeset-able. I get the feeling it is a future
problem solver.

Also note I'm ignoring all possible error checking, e.g. if $1 is not
an identifier (in the worst case, contains an "="), things go badly.

Sure, it's naive at this point.  I don't yet think past that.
I'm not exactly sure what you're wanting as either output or exit
status here, but except that I'd recommend [[ ]] instead of [ ] as
the test syntax, what you wrote for "this works" is sensible.

Ok.  I don't pay enough attention to the difference.

BTW, as a point of list etiquette: Responding to most posts I let my lines wrap
but resonding to yours, which seem to alway have fixed line width, I try to
match that style, but looking at the returned posts from the list, they sometimes
sproing badly. Should I  try to keep to your width, or just let lines wrap?




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