Tables synchronizing with each other

  1. What do you want to achieve?
    I was trying to create a function that returns a 3 dimensional array.
    This is the code:
function three_dimensional_array(x:number, y:number, z:number)
	local axis_table = {[0] = 0}
	for ai, axis in pairs({x, y, z}) do
		axis_table[ai] = {}
		for i = 1, axis do
			table.insert(axis_table[ai], axis_table[ai-1])
		end
	end
	return axis_table[3]
end
  1. What is the issue?
    Whenever I change a value inside the array this happens
a_table = three_dimensional_array(3, 3, 3)
a_table[1][1][1] = "hi"
print(a_table)

Output:


{
	[1] = {
		[1] = {
			[1] = "hi",
			[2] = 0,
			[3] = 0
		},
		[2] = {
			[1] = "hi",
			[2] = 0,
			[3] = 0
		},
		[3] = {
			[1] = "hi",
			[2] = 0,
			[3] = 0
		}
	},
	[2] = {
		[1] = {
			[1] = "hi",
			[2] = 0,
			[3] = 0
		},
		[2] = {
			[1] = "hi",
			[2] = 0,
			[3] = 0
		},
		[3] = {
			[1] = "hi",
			[2] = 0,
			[3] = 0
		}
	},
	[3] = {
		[1] = {
			[1] = "hi",
			[2] = 0,
			[3] = 0
		},
		[2] = {
			[1] = "hi",
			[2] = 0,
			[3] = 0
		},
		[3] = {
			[1] = "hi",
			[2] = 0,
			[3] = 0
		}
	}
}

As you can see the “hi” repeated because the tables are synchronizing.

  1. What solutions have you tried so far?
    I tried unpacking the table and {} it together but it doesn’t work for tables inside of tables

Can’t you do:

local newtable = {}
for i,v in pairs(thetable["thetableinsidethetable"]) do
	table.insert(newtable, v)
end
table.insert(axis_table[ai], axis_table[ai-1])

You are inserting a reference to the previous table, in the current table.
Use {} instead of axis_table[ai-1].

1 Like

Something like this?

table.insert(axis_table[ai], {table.unpack(axis_table[ai-1])})
1 Like

Your code has a fixed amount of parameters, why do you need a loop for the dimensions when you know they will always be 3?

Here is a simpler to understand version:

function three_dimensional_array(xsize: number, ysize: number, zsize: number)
	local array = {}
	
	for x = 1, xsize do
		array[x] = {}
		for y = 1, ysize do
			-- create a table with size zsize and populate it with value 0
			array[x][y] = table.create(zsize, 0)
		end
	end
	
	return array
end

Your function created a table, populates it with previous element in the parent table, which at the start is 0, but after that it becomes the latest populated table, so you essentially start creating tables full of references to the previous table.

1 Like

Nope, same result as

table.insert({table.unpack(thetable)})

Output:

{
	[1] = {
		[1] = {
			[1] = "hi",
			[2] = 0,
			[3] = 0
		},
		[2] = {
			[1] = 0,
			[2] = 0,
			[3] = 0
		},
		[3] = {
			[1] = 0,
			[2] = 0,
			[3] = 0
		}
	},
	[2] = {
		[1] = {
			[1] = "hi",
			[2] = 0,
			[3] = 0
		},
		[2] = {
			[1] = 0,
			[2] = 0,
			[3] = 0
		},
		[3] = {
			[1] = 0,
			[2] = 0,
			[3] = 0
		}
	},
	[3] = {
		[1] = {
			[1] = "hi",
			[2] = 0,
			[3] = 0
		},
		[2] = {
			[1] = 0,
			[2] = 0,
			[3] = 0
		},
		[3] = {
			[1] = 0,
			[2] = 0,
			[3] = 0
		}
	}
}

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.