Strange behaviour in dictionary; for loop is setting too many values

Hey there! I have a problem with adding values into my dictionary.
See the following code

local venuesClaimed = {
        --Each entry's index is a column
		[1] = {
			--Each entry stands for a row and whether it is occupied ({}) or not (Instance)
			[1] = Instance
			[2] = {};
			[3] = Instance;
			...
			[50] = {};
		};
		[2] = {
				[1] = {};
				[2] = Instance;
				[3] = {};
				...
				[50] = {}
			};
		
			...
		
		[50] = {
			...
		};
}

local allColumnsToOccupy = {24,25,26,27,28,29}
local allRowsToOccupy = {47}
local testInstance = part

for i, column in pairs(allColumnsToOccupy) do
	
	for j, row in pairs(allRowsToOccupy) do
		if venuesClaimed[column] then
			venuesClaimed[column][row] = testInstance
		end	
	end
	
end

I would expect the code to set the value of each row in the allRowsToOccupy table to testInstance (only in the row dictionaries which index is in the allColumnsToOccupy table).
The result that I get is that every row dictionary from all of the columns in venuesClaimed are set. Why is this happening? I’ve spent hours trying to figure this out but I can’t seem to understand why this is happening… Btw, if I explained this too vague, please let me know! :slight_smile:

So this is what I get: (every single column’s row dictionary gets the new value)

Could you post the code that populates venuesClaimed? I suspect that each column references the same table.

1 Like

Can you specify what code you need a little bit more? I don’t understand

issue is that you’re using pairs since these are numerically indexed tables pairs may not iterate in the expected order
just replace pairs with ipairs

for _, column in ipairs(allColumnsToOccupy) do
    for _, row in ipairs(allRowsToOccupy) do
        if venuesClaimed[column] then
            venuesClaimed[column][row] = testInstance
        end 
    end
end

Unfortunately, this does not work. Can you think of any other possible solutions?

The code that initializes all of the column/row tables in venuesClaimed.