Is there a direct way to insert a dictionary into a table to reference back to. EX:
local MyTable = {} table.insert(MyTable, 1, key = 1) --clearly wont work. but i just wanted to know if there was a good way to insert a dictionary into a table
Adding a dictionary to a table can be done by indexing the table with the key:
local MyTable = {} --New table reference MyTable.key = 1 print(MyTable.key) --Prints 1 in output
table.insert()
is used to add a value to an array. An array is a numerically-indexed table. It would look like:
local array = {11, 12, 13, 14} print(array[3]) --Would print 13 in the output.
You don't use table.insert()
to add a key-value pair to a table.
Edit:
You would give an index in the existing table to reference a new table. Then you use the previous method to set any keys with their values.
local myTable = { someKey = 3 } myTable.newTable = {} --construct table in existing table under an index myTable.newTable.Key = 1 --Same as my answer before print(myTable.newTable.Key) --Prints 1 to output