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

Re: [PATCH] (?) typeset array[position=index]=value



On 6/5/21, Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> On Fri, Jun 4, 2021 at 9:29 PM Mikael Magnusson <mikachu@xxxxxxxxx> wrote:
>>
>> Relatedly, this also seems very inconsistent:
> [...]
>> So for regular arrays, unset will just set the element to the empty
>> string, for assoc arrays it removes the key and the value.
>
> That's because zsh doesn't support sparse arrays, and the NULL element
> indicates the end of the array, so you can't put a NULL element in the
> middle.  If we'd thought about it long ago, it might be an error to
> unset a regular array element, but we're stuck now.
>
>> For regular arrays, assigning () to the element unsets(?) the element
>
> No.  For regular arrays assigning an array to an element splices the
> rvalue array into the lvalue array.   Splicing the empty array
> shortens the regular array, it doesn't cause elements to become unset
> (unless you consider that the former $#'th element is now unset
> because that position no longer exists).

I just happened to come across the following, and I'm struggling to
fit the result into any sort of logic:
% a=( {1..50} )
% unset 'a[5,45]'
% typeset -p a
typeset -a a=( 1 2 3 4 '' 46 47 48 49 50 )

So did we just remove the given elements, or set them to the empty
string? Yes, we did both :).

(note, this isn't intended as an argument for or against any previous
points, i'm just mentioning it because it still seems inconsistent to
me)

-- 
Mikael Magnusson




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