Dumb question but I don’t know why this behavior happens. Basically, I am trying to make subtables with table.create and insert an array into one subtable. I have this example,
function insert(tabl, array)
for i = 1, 1 do
local currentIteration = tabl[i]
print(currentIteration)
local value1 = math.random(1, #array)
local value2 = math.random(1, #array)
local newValue1 = array[value1]
local newValue2 = array[value2]
table.insert(currentIteration, 1, newValue1)
table.insert(currentIteration, 1, newValue2)
print(tabl)
end
end
It’s suppose to print this
[1] = ▼ { -- without table.create
[1] = "A Value",
[2] = "Another Value"
},
[2] = {},
[3] = {},
[4] = {},
[5] = {},
[6] = {},
[7] = {},
[8] = {}
but instead prints this
[1] = ▼ { -- with table.create
[1] = "A Value",
[2] = "Another Value"
},
[2] = ▼ {
[1] = "A Value",
[2] = "Another Value"
},
[3] = ▼ {
[1] = "A Value",
[2] = "Another Value"
},
[4] = ▼ {
[1] = "A Value",
[2] = "Another Value"
},
[5] = ▼ {
[1] = "A Value",
[2] = "Another Value"
},
[6] = ▼ {
[1] = "A Value",
[2] = "Another Value"
},
[7] = ▼ {
[1] = "A Value",
[2] = "Another Value"
},
[8] = ▼ {
[1] = "A Value",
[2] = "Another Value"
}
Not sure why it inserts into all of the subtables with table.create… Can anyone explain the behavior behind this? I looked on the wiki but didn’t understand it much.
Here are the tables for reference
local newTable = table.create(8, {})
local Table = {
[1] = {};
[2] = {};
[3] = {};
[4] = {};
[5] = {};
[6] = {};
[7] = {};
[8] = {};
}
local AnotherTable = {{}, {}, {}, {}, {}, {}, {}, {}}