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 1/19/24, Stephane Chazelas <stephane@xxxxxxxxxxxx> wrote:
> 2024-01-18 18:58:41 -0800, Ray Andrews:
>>
>> On 2024-01-18 18:46, Bart Schaefer wrote:
>> > On Thu, Jan 18, 2024 at 3:08 PM Ray Andrews <rayandrews@xxxxxxxxxxx>
>> > wrote:
>> > > How hard would it be to get the first hash 'right'?
>> > That's not how hash tables work.
>>
>> I have no idea how they work which is why my comments have little value.
>> If
>> ksh can do it then it can be done, but as I said, it simply might not be
>> worth the effort and I'm in no position to judge the matter. I wish I'd
>> gotten that far in C, I'd heard of hash tables but never coded one. But
>> I
>> did note in that code previous that the order seemed not to change.
>> Nevermind.
>
> As said before, in "${!hash[@]}", ksh93 orders the keys
> lexically, not in the order they were inserted in the hash
> table. It doesn't record that order either.
>
> If you want to do the same in zsh, use the o parameter expansion
> flag to order the keys. See also n to order numerically:
Unfortunately, combining o with kv doesn't work as one might hope:
% typeset -A foo; foo=(a b c d e f g h 1 2 3 4 i 5 j 6 7 k 8 l)
% printf '%s = %s\n' ${(kv)foo}
3 = 4
g = h
i = 5
7 = k
j = 6
8 = l
a = b
c = d
1 = 2
e = f
% printf '%s = %s\n' ${(okv)foo}
1 = 2
3 = 4
5 = 6
7 = 8
a = b
c = d
e = f
g = h
i = j
k = l
I guess this is actually a bug since the manual states:
v Used with k, substitute (as two consecutive words) both the
key and the value of each associative array element. Used with
subscripts, force values to be substituted even if the subscript form
refers to indices or keys.
--
Mikael Magnusson
Messages sorted by:
Reverse Date,
Date,
Thread,
Author