Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Empty element elision and associative arrays (was Re: Slurping a file)
On Sun, Jan 14, 2024 at 8:09 PM Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:
>
> Exactly as I understand it. A arrays are 'not very clever' -- they don't try to protect you from yourself, there's no internal place holding for a missing value, and as you say things must be kept to pairs.
There is an alternate (in zsh, it's the default in e.g. ksh)
assignment syntax for associative arrays:
asc=( [one]=1 [two]=2 [nil]= )
The "not clever" pairwise assignment is a concession to zsh's base
practice of always passing expansions by value rather than by
reference. That is, you can not do
typeset -A asc=$otherasc
because the defined expansion of $otherasc is an array of values, not
an object reference that can be assigned in toto.
> One might think that the order of assignment would be 'the order' by inevitability
Associative arrays are maintained as hash tables for fast access.
Expansion is in hash key order, and the internal hash keys are
computed directly from the array key. Anything else requires
performing a sort at expansion time, and/or (especially if order of
assignment is to be preserved) storing extra data about the order
(such as a linked list through the hash elements). Zsh opted to do
neither of these on the premise that access to individual elements by
key is far more frequent than access to the entire table.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author