Can someone explain table.create behavior?

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 = {{}, {}, {}, {}, {}, {}, {}, {}}
1 Like

table.create uses 2 arguments you give it, the count, and something to use. It makes a table with the length of count that for each index contains the something you gave it, example

local tbl = table.create(5,"Test")
print(tbl) --prints {"Test","Test","Test","Test","Test"}

So if you gave it a subtable, it’s going to put that subtable into every index of the table

Oh so it’s referencing each value you gave it? Because when I print this in the first example,

print(currentIteration)

it prints only one sub table so I was confused on that behavior. So to clarify it references each value you assign to it?

Basically, like how I did in my example, I gave it 5 as the count and "Test" as the 2nd argument, which basically means, make a table of 5 indexes/length of 5 with every value being “Test”

Actually wait now I’m confused by what you mean by referencing each value given

table.create(5, {})

and

table = {{}, {}, {}, {}, {}}

So what I meant by each value given is in the second example in this reply I have 5 subtables. And if I insert a value from my above code, it’ll print the first example. in og post What I want. It only reads that 1 subtable.

But with table.create, I assume that the 2nd arg takes into account every value instead of 1 value. If that makes sense when I said

I probably meant index so sorry for the confusion.

If I understand you correctly then yea the 2nd arg takes into account every value instead of 1 value, least I think so, I don’t have that much experience with table.create, I’m just going off of what the documentation mentions, so sorry if I’m not being that much helpful, someone may be able to give a better answer to what you wanted

1 Like

In Lua, tables are passed-by-pointer/passed-by-reference therefore {} is basically a pointer (reference) to an empty table therefore table.create(5, {}) is essentially

local x = {}
return {x, x, x, x, x}

They’re not passed by value which you seemed to expect it to be. They’re not copied. You’re modifying pointer to the table and not the table as a value.


You gave a pass-by-value example, the OP has a problem where the 2nd argument is pass-by-pointer.

4 Likes

Terribly sorry if I gave the wrong examples to OP! I’ll let you continue to help him so I don’t accidentally give another bad example haha, best of luck!