So, I have a table that looks like this:
local Mytable ={ ["Money"] = 500, ["Inventory"] ={ ["Bread"] = 1, ["Apples"] = 40, }, }
So i'm trying to insert another Dictionary like this:
["Pets"] ={ ["Dogs"] = 1, ["Cats"] = 5, },
But how would I insert that Dictionary into Mytable? I already tried :
table.insert(Mytable,["Pets"] ={ ["Dogs"] = 1, ["Cats"] = 5, },)
But that didn't work... Anyone know how to do this?
Great question. As you already know, table.insert()
will not work with dictionaries! To add values or definitions to your table, there are 2 ways.
1st way:
local Mytable ={ ["Money"] = 500, ["Inventory"] ={ ["Bread"] = 1, ["Apples"] = 40, }, } Mytable.Color = "Green"
2nd way:
local Mytable ={ ["Money"] = 500, ["Inventory"] ={ ["Bread"] = 1, ["Apples"] = 40, }, } Mytable['Color'] = "Green"
Adding values to a dictionary is indexing it, which is practically the same way you define it! Hope I've helped.