Why do pairs iterate randomly?

How to do it in order?

pairs iterates randomly because when it gets a table, there’s just keys and values. How could there possibly be an order in a table? eg:

local t = {bear = "roar", dog = "woof", cat = "meow", duck = "quack"}

There’s no order in t, so pairs has nothing else to do but iterate randomly. But in arrays, there is an order. eg:

local arr = {"roar","woof","meow","quack"}

Why? Because the indexes are numbered, so ipairs can iterate in order.

if you want to iterate over a table in a specific order, you can do it like this:

local t = {bear = "roar", dog = "woof", cat = "meow", duck = "quack"}
local order = {"bear", "dog", "duck", "cat"}

for i,v in ipairs(order) do
    print(t[v])
end

--[[ prints:
-roar
-woof
-quack
-meow
--]]

See how the order it printed it the same as the order in order?

…or you could just use arrays

Here’s some examples to print a keyed table sorted by value or key.

function setup()
    t = {bear="roar",mouse="squeek",dog="woof",cat="meow",duck="quack"}
    key={}
    value={}
    for a,b in pairs(t) do
        table.insert(key,a)
        table.insert(value,b)
    end

    print("value sorted by value")
    table.sort(value)
    for a,b in pairs(value) do
        print("value = "..b)
    end
    
    print("\
\
value sorted by key")
    table.sort(key)
    for a,b in pairs(key) do
        print("key = "..b," value = "..t[b])
    end
end

It would be great if Codea Craft will have Table Logs:
Eg

t = {7, 2, 9}
t.x = 5
table.insert(t, 6)
t.r = 22

(PAIRS: a, b)
1, 7
2, 2
3, 9
x, 5
4, 6
r, 22

Codea Craft isn’t going to change any Lua functions

:frowning: - It’s not my popularity, and not yours! Simeon says final words

I am just using common sense, actually. If they changed this anywhere, it would be in Codea, but that would then be different to Lua, which could confuse people.

What you should do when you get stuck with a Lua problem, is google it first, you will find lots of explanations, so you don’t need to bother people here.

But Codea already is not Lua, when for example IO, require doesn’t work as shown in the web.

You have no idea what Codea Craft is about, do you?

You have no ideas what I said, do you?

@TokOut You’re correct when you say Codea is not Lua, but then Lua is not Codea. Codea took the basics of Lua as a foundation, then added all of the graphics, touches, etc that Lua doesn’t have so it works with iPad, iPhones, etc. When you look at the Lua language, you’ll find a lot of things that aren’t included in Codea. The developers of Codea didn’t think some things were necessary, so those weren’t included. When you look at Lua and say, that doesn’t work in Codea, then accept the fact that it doesn’t because it wasn’t meant to. We’re not trying to be rude to you, but you can look up information just as easy as we can. Besides, you know exactly what you don’t understand, we don’t. We can spend a half hour searching and giving you an answer, when it’s possible you’ll get your answer within a minute if you search yourself. A lot of things aren’t a yes or no answer and it takes time to find it and key it in so you can just read it.