Comparing tables with subtables

I’ve searched the forums on comparing tables and I have found some methods but uses JSONEncode.

I was wondering if there is an easier way to compare tables rather then:

--> For every item in the current inventory do...
		for _, item in pairs(ListInventory:GetChildren()) do
			--> Now we loop through player inventory...
			local exist = false
			
			--> Grabbing those tables
			for _, invItem in pairs(PlayerData.Inventory) do
				
				--> now we start loopin through the properties...
				local similar = true
				for property, value in pairs(invItem) do
					if typeof(value) ~= "table" then
						if value ~= item[property] then
							similar = false
						end
					else
						for subproperty, subvalue in pairs(value) do
							if typeof(subvalue) ~= "table" then
								if subvalue ~= item[property][subproperty] then
									similar = false
								end
							else
								for subsubproperty, subsubvalue in pairs(subvalue) do
									if subsubvalue ~= item[property][subproperty][subsubproperty] then
										similar = false
									end
								end
							end
						end
					end
				end
			end
		end
		

I was thinking of creating a function but then I would still find my self nesting more and more if statements.

I’m not sure if there is a way to go through everything inside a table (including subvalues) as well.

1 Like

For what you’re trying to do, I would recommend using a recursive function that loops through the table and calls itself every time it finds a value in the table that refers to another table, to loop through that one as well. I can write out the code a little later if you need it, but does that make sense?

1 Like

I’ve tried:

function hi()
    hi()
    wait(2)
    print("Hi")
end

hi()

Got:

 18:14:58.292 - ServerScriptService.Script:2: stack overflow
18:14:58.518 - Stack Begin
18:15:14.243 - Script 'ServerScriptService.Script', Line 2 - global hi (x16382)
18:15:14.244 - Script 'ServerScriptService.Script', Line 7
18:15:14.244 - Stack End
1 Like

You need some kind of end case, otherwise it runs infinitely.

2 Likes

any info why you’re trying to compare two tables? maybe there’s a different solution

1 Like

I have an inventory system on the client and the client has the ability to use items, since my items have many subtables and subvalues I need to make sure the item is exact.

This checks if the item exists and if it’s the correct item before using it.

1 Like

if anyone else has wants an idea of how to make this function, i made one here:

function matchTables(tbl1, tbl2)
    local function scanTable(tbl1, tbl2)
        for index, value in pairs(tbl1) do
            if type(value) ~= "table" then
                if value ~= tbl2[index] then return false end
            elseif type(value) == "table" and type(tbl2[index]) == "table" then
                return scanTable(value, tbl2[index])
            else
                return false
            end
        end
        return true
    end
    return scanTable(tbl1, tbl2)
end
2 Likes

Another solution I thought of is to salvage each table merging each index (so this way I can compare easily) with this:


module.salvageTables = function(Table)
	local salvagedContents = {}
	
	for Index, Value in Pairs(Table) do
		if TypeOf(Value) ~= "table" then
			salvagedContents[Index] = Value
		else
			local SalvagedContents = module.salvageTables(Value)
			if SalvagedContents then
				for subIndex, subValue in Pairs(SalvagedContents) do
					salvagedContents[Index..subIndex] = subValue
				end
			end
		end
	end
	for i, v in pairs(salvagedContents) do
		print(i, v)
	end
	return salvagedContents
end

module.salvageTables({
	["Test"] = 1,
	["Test2"] = {
		["Test1"] = 1,	
		["Test3"] = {
			["Test5"] = 5	
		}
	}	
})

The current issue is:

  Test5 5
  Test3Test5 5
  Test1 1
  Test 1
  Test2Test1 1
  Test2Test3Test5 5

As you can see the subvalues are also placed individually which isn’t what I want.

What I want is:

Test 1
Test2Test1 1
Test2Test3Test5 = 5

do you think you can explain what you mean by salvage?

It’s basically taking all the components in a table and then breaking it down into 1 table but then the indexes basically stack up like so.

This way It will only 1 index and value instead of multiple subscripts

EDIT: It’s kind of like a dot operator.

Good news! I’ve found a way to compare tables strictly:

It was this simple and yet I struggled ;-;

2 Likes