* How to preserve order of associative array?
@ 2015-11-07 16:11 Dominik Ritter
2015-11-07 17:08 ` Mikael Magnusson
2015-11-07 17:51 ` Bart Schaefer
0 siblings, 2 replies; 5+ messages in thread
From: Dominik Ritter @ 2015-11-07 16:11 UTC (permalink / raw)
To: zsh-users
Hi everyone!
I struggle to the order of an associative array right. If I take the
example from zsh-lovers man page:
$ typeset -A ass_array; ass_array=(one 1 two 2 three 3 four 4)
$ print ${(k)ass_array} # prints keys
one four two three
$ print ${(v)ass_array} # prints values
1 4 2 3
I would expect that to be the keys sorted as they appear in the array
(one, two, three, four).
I tried to sort them in array index order, but without luck:
print ${(ak)ass_array}
The other sort-modifiers won't help me, as I need a hierarchy in my
array keys (neither lexically, nor numerical).
Thanks for any help. Regards,
Dominik
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: How to preserve order of associative array?
2015-11-07 16:11 How to preserve order of associative array? Dominik Ritter
@ 2015-11-07 17:08 ` Mikael Magnusson
2015-11-07 17:51 ` Bart Schaefer
1 sibling, 0 replies; 5+ messages in thread
From: Mikael Magnusson @ 2015-11-07 17:08 UTC (permalink / raw)
To: Dominik Ritter; +Cc: Zsh Users
On Sat, Nov 7, 2015 at 5:11 PM, Dominik Ritter <dritter03@googlemail.com> wrote:
> Hi everyone!
>
> I struggle to the order of an associative array right.
Associative arrays are unordered.
--
Mikael Magnusson
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: How to preserve order of associative array?
2015-11-07 16:11 How to preserve order of associative array? Dominik Ritter
2015-11-07 17:08 ` Mikael Magnusson
@ 2015-11-07 17:51 ` Bart Schaefer
2015-11-10 20:52 ` Dominik Ritter
1 sibling, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2015-11-07 17:51 UTC (permalink / raw)
To: zsh-users
On Nov 7, 5:11pm, Dominik Ritter wrote:
}
} I struggle to the order of an associative array right.
Second paragraph in the doc under "Parameters":
The attributes of a parameter determine the _type_ of its value, often
referred to as the parameter type or variable type, and also control
other processing that may be applied to the value when it is referenced.
The value type may be a _scalar_ (a string, an integer, or a floating
point number), an array (indexed numerically), or an _associative_
array (an unordered set of name-value pairs, indexed by name, also
referred to as a _hash_).
You can simulate an ordered set of name-value pairs like this (need zsh
version 5.1 for assigning to an array during typeset):
typeset -a ord_array=(one 1 two 2 three 3 four 4)
print ${ord_array[ord_array[(i)three]+1]}
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: How to preserve order of associative array?
2015-11-07 17:51 ` Bart Schaefer
@ 2015-11-10 20:52 ` Dominik Ritter
2015-11-11 0:59 ` Bart Schaefer
0 siblings, 1 reply; 5+ messages in thread
From: Dominik Ritter @ 2015-11-10 20:52 UTC (permalink / raw)
To: Bart Schaefer; +Cc: zsh-users
> The attributes of a parameter determine the _type_ of its value, often
> referred to as the parameter type or variable type, and also control
> other processing that may be applied to the value when it is referenced.
> The value type may be a _scalar_ (a string, an integer, or a floating
> point number), an array (indexed numerically), or an _associative_
> array (an unordered set of name-value pairs, indexed by name, also
> referred to as a _hash_).
Thank you very much. That explains it. I've overseen that in the documentation.
> You can simulate an ordered set of name-value pairs like this (need zsh
> version 5.1 for assigning to an array during typeset):
>
> typeset -a ord_array=(one 1 two 2 three 3 four 4)
> print ${ord_array[ord_array[(i)three]+1]}
Perfect! I'll try that. Thanks.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: How to preserve order of associative array?
2015-11-10 20:52 ` Dominik Ritter
@ 2015-11-11 0:59 ` Bart Schaefer
0 siblings, 0 replies; 5+ messages in thread
From: Bart Schaefer @ 2015-11-11 0:59 UTC (permalink / raw)
To: zsh-users
On Nov 10, 9:52pm, Dominik Ritter wrote:
}
} > typeset -a ord_array=(one 1 two 2 three 3 four 4)
} > print ${ord_array[ord_array[(i)three]+1]}
}
} Perfect! I'll try that. Thanks.
I should note that this can fail if the same strings can appear as both
keys and values.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2015-11-11 0:59 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-11-07 16:11 How to preserve order of associative array? Dominik Ritter
2015-11-07 17:08 ` Mikael Magnusson
2015-11-07 17:51 ` Bart Schaefer
2015-11-10 20:52 ` Dominik Ritter
2015-11-11 0:59 ` Bart Schaefer
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).