Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to print a table in order?

Asked by 5 years ago

Hello, I made this table and I want to print it in order, how would I do this?

local tableThing = {
hello = 'Hello',
hi = 'Hi',
hai = 'Hai'}

This is just an example. I attempted this but it printed them randomly, not in order.

for i, child in pairs(tableThing) do
print(child)
end
0
I'm no good with tables aha CrazyCorrs 98 — 5y

1 answer

Log in to vote
0
Answered by
Azarth 3141 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

You don't. Dictionaries are unordered sets, unlike normal arrays, which have indexes. Without another function to order your dictionary, it won't do it automatically. I would just not use a dictionary in your case.

local t = {'hi', 'hello'}
print ( table.concat(t, ', ') )

or

local t = {
    {name = 'hi', othervalue = 1}, 
    {name = 'hello', othervalue = 'string'}
}

table.insert(t, {name = 'harry potter', othervalue = true})

for i,v in pairs(t) do 
    print(v.name)
end
Ad

Answer this question