feature proposal: expand associative arrays in lexicographic order of keys

Zachary Santer <[email protected]>
Newsgroups gmane.comp.shells.bash.bugs
Message-ID <CABkLJU+b+3=VYw0vntNwzj=yafw0aNhySL-YvQYi4tuDD0944Q@mail.gmail.com>
given the current locale's collation order.

I'm actually struggling to find where the devel branch's doc/bash.1
bothers to say that the expansion of indexed arrays subscripted by @
or * is in numerical order of indices and the expansion of associative
arrays subscripted by @ or * is in arbitrary order.

When expansion order matters at all, this is liable to be what the
shell programmer would prefer. ${!assoc[@]} expanding in lexicographic
order would make for a slight improvement in one of my scripts, at
least.

I can't imagine a scenario where a script would depend upon an
associative array expanding in not necessarily a sorted order, but
this functionality could be controlled by a shopt option. Maybe it
would save a few clock cycles if the programmer doesn't care. On the
other hand, how would bash handle this shopt option being enabled
after associative arrays have already been assigned to?

Given there's no "requirement that members be indexed or assigned
contiguously", I assume bash maintains indexed array elements in a
linked list, so the array doesn't have to be sorted by index every
time you expand it. Notwithstanding that the elements may be found
using a hash table when they're referenced individually.

So that linked list would have to be maintained throughout the
lifetime of the indexed array. The same would go for associative
arrays with keys sorted. So if this is controlled by a shopt option,
would bash need to sort all the existing associative arrays when the
option is enabled? How about changing LC_ALL or LC_COLLATE in the
middle of things?

Some locales weight different strings equally, so that would add
another little wrinkle. Elements with indices with equal lexicographic
weight could be placed in arbitrary order, relative to each other. The
shell programmer could always set LC_COLLATE to C.UTF-8 if it matters
that much to them.

Thoughts?

Zack
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.