The way you are using this table is as a dictionary. Dictionaries don’t have any concrete order for what is next in the table, only arrays have this quality. You could test this by say, running a pairs
loop that prints the key/value in the dictionary, and do this loop 3 times. Here’s an example script showing what I mean:
local t = {["k1"] = 1, ["k2"] = 2, ["k3"] = 3}
for i = 1, 3 do
print("start print ".. i)
for k,v in pairs(t) do
print(k,v)
end
end
You would see that the key/value pairs are printing in a random order each time instead of in a specified order, like "k1"
, "k2"
, "k3"
.
So, even if you had your original table here:
And you printed its key/value pairs out three different times, you would still end up with a random order. In conclusion, it shouldn’t matter how you put "example2"
in the table, it would still be in no specified order.
If you wanted an order to the dictionary, you would either use a numerical for
loop, or you would place them in a two-dimensional array and run a for
loop through that table.
The two solutions up there:
for i = 1, 3 do
local key = "example" .. i
print(exampletable[key])
end
local exampletable = {
{ Key = "example1", Value = {"1","2"} },
{ Key = "example2", Value = {"1","2"} },
{ Key = "example3", Value = {"1","2"} }
}
for i,v in ipairs(exampletable) do
print(i, v.Key, v.Value)
end