Thread overview
unordered output of an associated array of associated arrays
Jan 25, 2022
forkit
Jan 25, 2022
H. S. Teoh
Jan 25, 2022
forkit
Jan 25, 2022
forkit
Jan 25, 2022
H. S. Teoh
Jan 25, 2022
forkit
Jan 25, 2022
H. S. Teoh
Jan 25, 2022
forkit
January 25, 2022
so I'm trying to understand why the output of the code below, is in reverse order of the declaration (and how to fix it so that it outputs in an ordered way)

i.e. output is:
typeA:
	A2:A2value
	A1:A1value
typeB:
	B3:B3value
	B2:B2value
	B1:B1value

// --

module test;

import std;

void main()
{
    string[string][string] aaTable =
     ([ "typeA" : ["A1" : "A1value", "A2" : "A2value"],
        "typeB" : ["B1" : "B1value", "B2" : "B2value", "B3" : "B3value"]
     ]);


    foreach (topLevelKey, topLevelValue; aaTable.byPair)
    {
        writefln("%s:", topLevelKey);

        foreach(key, value; topLevelValue)
        {
            writefln("\t%s:%s", key, value);
        }
    }

}

//--

January 24, 2022
On Tue, Jan 25, 2022 at 12:23:40AM +0000, forkit via Digitalmars-d-learn wrote:
> so I'm trying to understand why the output of the code below, is in reverse order of the declaration (and how to fix it so that it outputs in an ordered way)

AA's are unordered containers. Do not rely on entries to appear in any specific order when you traverse an AA; it is implementation-dependent and may differ from OS to OS / platform to platform / sequence of operations performed on the AA since its initialization / phase of the moon. Any specific order that may appear in an AA is pure coincidence and may not appear again next time, or may only appear again at 11:59:59 Feb 29 under a blue moon.

If you need entries in your AA in a specific order, extract them (or their keys) into an array then sort them yourself. E.g.:

	string[string] myAA;
	auto keys = myAA.keys;
	sort(keys);
	foreach (k; keys) {
		... // now keys will be in the expected order
	}


T

-- 
Freedom of speech: the whole world has no right *not* to hear my spouting off!
January 25, 2022
On Tuesday, 25 January 2022 at 00:23:40 UTC, forkit wrote:
>
another example:

output is:
typeA:
	100000001:[0, 0, 1, 1, 1, 1, 1, 1]
	100000002:[0, 0, 0, 1, 1, 1, 1, 1]
typeB:
	100000005:[0, 0, 0, 0, 0, 0, 1, 1]
	100000003:[0, 0, 0, 0, 1, 1, 1, 1]
	100000004:[0, 0, 0, 0, 0, 1, 1, 1]


// --

module test;

import std;

void main()
{
    auto aaTable2 =
      ([
       "typeA" : [ 100000001 : [0, 0, 1, 1, 1, 1, 1, 1],
                   100000002 : [0, 0, 0, 1, 1, 1, 1, 1] ],
       "typeB" : [ 100000003 : [0, 0, 0, 0, 1, 1, 1, 1],
                   100000004 : [0, 0, 0, 0, 0, 1, 1, 1],
                   100000005 : [0, 0, 0, 0, 0, 0, 1, 1] ]
                 ]);

    foreach (topLevelKey, topLevelValue; aaTable2.byPair)
    {
        writefln("%s:", topLevelKey);

        foreach(key, value; topLevelValue)
        {
            writefln("\t%s:%s", key, value);
        }
    }

}

//--



January 25, 2022
On Tuesday, 25 January 2022 at 00:39:05 UTC, H. S. Teoh wrote:
>
>
> AA's are unordered containers. Do not rely on entries to appear in any specific order when you traverse an AA; it is implementation-dependent and may differ from OS to OS / platform to platform / sequence of operations performed on the AA since its initialization / phase of the moon. Any specific order that may appear in an AA is pure coincidence and may not appear again next time, or may only appear again at 11:59:59 Feb 29 under a blue moon.
>
> If you need entries in your AA in a specific order, extract them (or their keys) into an array then sort them yourself. E.g.:
>
> 	string[string] myAA;
> 	auto keys = myAA.keys;
> 	sort(keys);
> 	foreach (k; keys) {
> 		... // now keys will be in the expected order
> 	}
>
>
> T

oh. thanks :-)

I will get that integrated into my example code, and will post again, once it's working (so others can  learn too)


January 25, 2022
On Tuesday, 25 January 2022 at 00:43:07 UTC, forkit wrote:
>
> oh. thanks :-)
>
> I will get that integrated into my example code, and will post again, once it's working (so others can  learn too)

ok.. so I took the easy way out ;-)

output is now ordered:

typeA:100000003:[1, 1, 1, 0, 0, 0, 0, 0]
typeA:100000004:[1, 1, 1, 1, 0, 0, 0, 0]
typeA:100000005:[1, 1, 1, 1, 1, 0, 0, 0]
typeB:100000001:[1, 0, 0, 0, 0, 0, 0, 0]
typeB:100000002:[1, 1, 0, 0, 0, 0, 0, 0]
typeC:100000006:[1, 1, 1, 1, 1, 1, 0, 0]
typeC:100000007:[1, 1, 1, 1, 1, 1, 0, 0]


// --
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);

}

// ---
January 24, 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
January 25, 2022
On Tuesday, 25 January 2022 at 02:12:50 UTC, H. S. Teoh wrote:
>
> 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

surely, this is voodoo?  ;-)
January 24, 2022
On Tue, Jan 25, 2022 at 02:46:43AM +0000, forkit via Digitalmars-d-learn wrote:
> On Tuesday, 25 January 2022 at 02:12:50 UTC, H. S. Teoh wrote:
> > 
> > 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]);
> > 		});
> > 	});
[...]
> surely, this is voodoo?  ;-)

No, this is D-doo.  We're here to do-D-doo. :-D


T

-- 
If you're not part of the solution, you're part of the precipitate.