unordered output of an associated array of associated arrays
H. S. Teoh
hsteoh at quickfur.ath.cx
Tue Jan 25 02:12:50 UTC 2022
On Tue, Jan 25, 2022 at 02:04:26AM +0000, forkit via Digitalmars-d-learn wrote:
[...]
> // --
> module test;
>
> import std;
>
> void main()
> {
> auto aaTable =
> ([
> "typeB" : [ 100000002 : [1, 1, 0, 0, 0, 0, 0, 0],
> 100000001 : [1, 0, 0, 0, 0, 0, 0, 0]
> ],
> "typeC" : [ 100000007 : [1, 1, 1, 1, 1, 1, 0, 0],
> 100000006 : [1, 1, 1, 1, 1, 1, 0, 0]
> ],
> "typeA" : [ 100000005 : [1, 1, 1, 1, 1, 0, 0, 0],
> 100000003 : [1, 1, 1, 0, 0, 0, 0, 0],
> 100000004 : [1, 1, 1, 1, 0, 0, 0, 0]
> ]
> ]);
>
> string[] orderedKeyPairSet;
>
> foreach (key, pair; aaTable.byPair)
> {
> foreach(k, p; pair.byPair)
> orderedKeyPairSet ~= key ~ ":" ~ k.to!string ~ ":" ~
> p.to!string;
> }
>
> orderedKeyPairSet.sort;
>
> foreach(s; orderedKeyPairSet)
> writeln(s);
That's the *easy* way out?? Try this instead:
aaTable.keys.sort.each!((k) {
aaTable[k].keys.sort.each!((kk) {
writefln("%s:%s:%s", k, kk, aaTable[k][kk]);
});
});
T
--
Try to keep an open mind, but not so open your brain falls out. -- theboz
More information about the Digitalmars-d-learn
mailing list