zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: zsh-users@zsh.org
Subject: Re: array prepend
Date: Wed, 16 Jan 2013 20:34:20 -0800	[thread overview]
Message-ID: <130116203420.ZM22356@torch.brasslantern.com> (raw)
In-Reply-To: <CACQNQ9MJ6_steMFsvqAWnqtYS4f3eRC2dOb_agyxqbqScyVwgA@mail.gmail.com>

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]}.


      reply	other threads:[~2013-01-17  4:34 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-01-11  6:58 sergio
2013-01-12  1:38 ` Oliver Kiddle
2013-01-13 18:51   ` Peter Stephenson
2013-01-14  4:48     ` rahul
2013-01-14 15:32       ` Bart Schaefer
2013-01-14 18:48         ` rahul
2013-01-17  4:34           ` Bart Schaefer [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=130116203420.ZM22356@torch.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).