Adding new data to dictionaries

I want to write some code that will add new data to a dictionary based off of another dictionary

local a = {
    Flowers = {
        "Rose",
        "Tulip",
    },
}

local b = {
    Flowers = {
        "Rose",
    },
}

What’s the easiest way I can check through dictionary b to add what it’s missing from dictionary a

	local Diff = 0 
        local Compareitems = {}
	for key1, value1 in pairs(Table) do local IsDifferent = false
          for key2, value2 in pairs(Table2) do
table.insert(Compareitems,#Compareitems+1,{key2,value2})
		if key1 == key2 then 
for i = 1,#Compareitems do 
local Item = Compareitems[i]
if Item[1] == key2 then
table.remove(Compareitems,i)
end
end
 else   
        end

        end
if IsDifferent then
Diff = Diff + 1

end
	end
	return Compareitems,Diff
end
-- Edit: I had the biggest brainfart while writing this.

You can just iterate through “a” and check the nodes in “b” (“a” is the default data from the system, “b” is the player data)

local a = {['YourNode1'] = 'YourValue1'; ['YourNode2'] = 'YourValue2'}
local b = {['YourNode2'] = 'RandomValue'}

for i,v in pairs(a) do
	if b[i] then
		-- HERE YOU KNOW A AND B HAS BOTH THE SAME NODE (KEY)
		print(b, 'has node',i,'with value',v)
	else
		-- HERE YOU KNOW IT DO NOT CONTAINS
		print(b, 'does not have node',i,'with value',v)
	end
end

I also use this in my data system which is used on a published game with over 300k+ visits so it’s battle tested and work 100%

2 Likes

Oh, dear. You’re right. You can just check the second dictionary for the key. Well I feel stupid.

np lmao we all learn something new at each day

yeah. But it isn’t really learning, is it…? it’s just me sucking at using information I had available.

Now you learned something new, the method you were using was bad because it has a time complexity of o(n²+1), mine has o(n) so it’s faster

I like this solution, however this won’t catch changes made to tables inside the dictionary

It catches if u make a recursive progression inside

1 Like

I suggest using metables for this.
So once a value changes an event will start.

1 Like

I know. Hence why I said I feel stupid, obviously, I was using a far inferior method.

And how should I do this? That’s what I originally tried to do but got tangled up in all of the Keys and indices. If you could post some code that would be a huge help.

Let me show you:

function update_structure(tbl,comparison_data)
	for i,v in pairs(comparison_data) do
		if tbl[i] and type(v) == 'table' then
			update_structure(tbl[i],comparison_data[i])
		else
			tbl[i] = v
		end
	end
end

update_structure(b,a)
1 Like

I like this solution too, however for my use case (player data stores) the code doesn’t change mid-game so the events would never fire.

You can use metatables on a data interface with back-end cache.

This way you edit only the interface but the interface never gets changed,all changes are applied to the back-end cache. I also have code for that :slight_smile:

function create_interface(tbl)
    local path = tbl
    local interface = setmetatable({},{
        __index = function(t,k)
            if rawget(tbl,k) then
                path = rawget(path,k)
                return create_interface(rawget(tbl,k))
            end
        end;
        __newindex = function(t,k,v)
            print('Value '..k..' changed.')
            rawset(path,k,v)
        end;
    })
    return interface
end

local data = override_data_store and (get_attempt(self.UserId,0,override_data_store) or default_data()) or get_attempt(self.UserId,0)  or default_data()
local interface = create_interface(data)

Can’t you just add to table a and table b the value at the same time?
Or just fire an event, Remote Event or even a function?

1 Like

Thatt does not even make sense because one of the tables represent the default structure of the player data which is the system structure, it is a non-object-oriented data.

1 Like

Btw charcle the metatable code I sent do work but you can read unless you do some sort of method to get the raw data just for read purposes.

Thanks to everyone who replied, this is a big help : )