How to designate position of a table with custom index inside another table?

So say I have a table:

``````local exampletable =
{
["example1"] = {"1","2"},
["example3"] = {"1","2"}
``````

And I want to put:

``````["example2"] ={"1","2"}
``````

After example 1.
I’m not sure how I would go about doing this, I know if I didn’t give the table index its own value but rather have it a number, I could just do

``````table.insert()
``````

But I’m not aware of any alternative that doesn’t use a number.

Dictionary keys don’t have any guaranteed traversal order. All you can do is `exampletable["example2"] = {"1", "2"}`

You can sort of simulate it by having two tables - one storing the keys and the other one as your dictionary. In that case, you could represent the order by the indices inside that keys table.

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"`.

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
``````