From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 28342 invoked by alias); 17 Jan 2013 04:34:25 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 17574 Received: (qmail 2158 invoked from network); 17 Jan 2013 04:34:24 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.2 Received-SPF: none (ns1.primenet.com.au: domain at closedmail.com does not designate permitted sender hosts) From: Bart Schaefer Message-id: <130116203420.ZM22356@torch.brasslantern.com> Date: Wed, 16 Jan 2013 20:34:20 -0800 In-reply-to: Comments: In reply to rahul "Re: array prepend" (Jan 15, 12:18am) References: <50EFB80D.30002@sergio.spb.ru> <22417.1357954693@thecus.kiddle.eu> <20130113185141.4394d532@pws-pc.ntlworld.com> <130114073236.ZM6828@torch.brasslantern.com> X-Mailer: OpenZMail Classic (0.9.2 24April2005) To: zsh-users@zsh.org Subject: Re: array prepend MIME-version: 1.0 Content-type: text/plain; charset=us-ascii On Jan 15, 12:18am, rahul wrote: } } I was hoping to assign lists (arrays) to an associative array, and got the } error which is well documented on the internet. But no one has given any } workaround or alternative to this. } } FOO[a]=("a command" "some text" "etc etc") } } zsh: FOO: attempt to set slice of associative array Right, that syntax means to take the element at [a] and splice the words inside the parens into the array at that position, replacing the value at [a]. You can't splice an associative array. } Is the only alternative to do the following: } } FOO_a=( a list) } FOO_b=(another list) It took me a while but I think I finally understand what you are asking about here. You want to implement a two-dimensional array. This is not directly supported in most shells (I don't know whether recent ksh has come up with some kind of syntax for it). The best you can do is to simulate the 2D array by placing a value at the position in the "outer" array which represents the name of another array that holds the "inner" array. E.g. to assign an array to FOO[a] you need something like typeset -i _x=0 typeset -A FOO set -A ${FOO[a]::=FOO$[++_x]} "a command" "some text" "etc etc" Then ${(P)FOO[a]} will retrieve the array. Of course, you also end up needing to fiddle with a few other conventions as well, e.g., you must do both unset $FOO[a] unset "FOO[a]" to delete the "outer" element and its value. This trick does not extend 2D associative arrays because ${(P)...} cannot be told how to retrieve a named element of the "inner" hash; it retrieves all the elements (in this case all the values in random order) and returns them as an ordinary array with positional index. I.e., ${(P)FOO[x][y]} is interpreted as ${${(@P)FOO[x]}[y]}.