Json encode / decode questions

I guess I tend to think of dictionaries as being somewhat unpredictable anyway - they have no order as such, in that you can’t predict what order pairs will return the items in.

@yojimbo2000 @dave1707 I am perpetually baffled by the fact that pairs() seems to be so inconsistent in its traversal of a table.

@Simeon @John I am curious how it is possible for pairs() based for loops to occur in different order each run?

I was using them to parse and layout GUI objects, and it was practically random order each run. This goes against everything I have ever seen in programming, usually the same scenario produces the exact same result…

I alwasy was assuming that pairs() would work alphabetically, or in the order that the values were created… Is this related to available memory locations / garbage collection / frame rate? It may be worth adding a note in the built in documentation, I was going bannanas trying to figure out why things were always showing up in a different order and ended up having to switch to ipairs to keep from losing my mind :P`

Super useful thread btw, some of this seems worthy of inclusion into a built in demo / example of some sort.

@AxiomCrux I think one of the reasons pairs works the way it does is because pairs will iterate thru a table even if it contains empty entries. For example if tab[5]=100 and tab[1000]=345, pairs will show both entries, but ipairs wont show either because it’s not consecutive starting at 1. You’re probably correct when you say that the tables are probably created based on available memory locations. I did a google search for pairs and found this:

Description

Returns an iterator function(next) for a for loop that will return the values of the specified table in an arbitrary order.

heheh yeah! @dave1707 “arbitrary order” thats the thing I can’t seem to fully wrap my head around…

I’ve been using computers since I could form memories, and I have recently come to understand cryptography and how ‘random’ number generation works (which opened my mind to profound philisophical insight into chaos theory and the nature of the universe).

“arbitrary order” and programming are opposing concepts IMHO. The notion that a key component of this awesome Lua programming language functions that way… . scrambles my brain into “arbitrary order” a bit.

@AxiomCrux Maybe you’ll find this interesting. It contains something about tables but I don’t know if it really answers anything about arbitrary order.

https://www.lua.org/gems/sample.pdf

Don’t think of it as “arbitrary order”, just unordered. JSON dictionaries are unordered as well. If order is important, you need to put the elements into an array.

@yojimbo2000 @AxiomCrux The comment I mentioned in the above post and what I show below is wrong for pairs. It won’t return the values in arbitrary order, but will create the table in arbitrary order. If you run the example code below and keep tapping the screen to read the table, it will show the same results in some order each time. If you restart the program, it will create the table in a different order.

Description

Returns an iterator function(next) for a for loop that will return the values of the specified table in an arbitrary order.
function setup()
    tab={a=1,b=2,c=3,d=4,e=5}
    for a,b in pairs(tab) do
        print(a,b)
    end
end

function touched(t)
    if t.state==BEGAN then
        output.clear()
        for a,b in pairs(tab) do
            print(a,b)
        end
    end
end