iterating through 2 tables while checking each value

Okay so I’m having a bit of trouble trying to figure out how to iterate through two tables to see if each key are the same, and if not insert the key and value from the table that has it, into the table that dosnt
for example: take it I have 2 tables

example tables:

My Code to iterate through both tables to add the missing data from one table


function GMI:ValBus(business)
	if table.Count(business) != table.Count(GMI.Businesses) then
		for gmi_k, gmi_v in pairs (GMI.Businesses) do
			for k,v in pairs (business) do
				if gmi_k != k then
					table.ForceInsert(business, gmi_k, gmi_v)
				end
			end
		end
	end
	return business
end

The reason on why I cant just go

, is because business is a variable for the player data business. Which im trying to check if the playerdata business has all the same businesses as the server(GMI.Business), and if it dosnt then add those businesses to the playerdata business table, without overriding the playerdata businesses, that the playerdata business already had. Because say if one player was playing with this and there business data was differnt I wouldnt want to override the whole table evertime a new business was add, i would just want to add the ones that the player dosnt have.

Use a metatable and the __index method.


local primary, alpha, beta

primary = {
    Fishing = { Name = "Fishing", Price = 5, },
    Mail = { Name = "Mail", Price = 10, },
    Movies = { Name = "Movies", Price = 15, },
    Babysitter = { Name = "Babysitter", Price = 20, },
}

alpha = { }

alpha.Businesses = setmetatable( { Valet = { Name = "Valet", Price = 25, } }, { __index = primary } )

beta = { }
beta.Businesses = setmetatable( { }, { __index = primary } )

primary.Corker = { Name = "Corker", Price = 99 }

print( alpha.Businesses.Fishing.Price )
print( alpha.Businesses.Valet.Price )
print( alpha.Businesses.Corker.Price )

print( beta.Businesses.Mail.Price )
print( tostring( beta.Businesses.Valet ) )

print( "Base" )
for k, v in pairs( primary ) do
    print( k, v.Name, v.Price )
end

print( "A" )
for k, v in pairs( alpha.Businesses ) do
    print( k, v.Name, v.Price )
end

print( "B" )
for k, v in pairs( beta.Businesses ) do
    print( k, v.Name, v.Price )
end


5
25
99
10
nil
Base
Movies    Movies    15
Corker    Corker    99
Mail    Mail    10
Fishing    Fishing    5
Babysitter    Babysitter    20
A
Valet    Valet    25
B

The example is a bit of a mess and the main flaw is that pairs/ipairs won’t see the inherited values ( if we can implement __pairs that could be resolved ), but the idea is that when the code tries to index alpha or beta, if it is nil it will ask primary for that key instead.

Of course, if your goal is to allow player A and B to have different values on the same key, you would copy the primary value to the player’s table and then make changes in their table so as not to adjust the base version.

How would I be able to check to see if table Alpha has a same key as in table Beta, and If it dosnt, than add that key value to table Alpha

Im just looking to be able to iterate through the 2 tables, and being able to check and see if both tables have the same keys. but when the keys don’t match or if one table has more keys than the other, add that key value to the table that dosnt have it.

this is my example


function GMI:ValBus(business)
	local getting_table_keys = table.GetKeys(business)
	local n = 1
	if table.Count(business) != table.Count(GMI.Businesses) then
		for gmi_k, gmi_v in pairs (GMI.Businesses) do
			if if gmi_k == getting_table_keys[n]
				n = n+1
			elseif gmi_k != getting_tablekeys[n] then
				table.ForceInsert(business, gmi_k, gmi_v)
				n = n+1
			end 
		end
	end
	return business
end

anyone know what I should do, I also am trying this

GMI is an index table



function GMI:ValBus(business)
for ply_k, ply_v in pairs (self.Businesses) do
	if not business[ply_k] then
		table.ForceInsert(business, ply_k,ply_v)
	end
end
	end