Create multiple keys from one function

So, I have a function that create’s a key inside a key inside a key which has a value

-- Like this:
-- key -> key -> key -> value
-- Better Example:
--[[
"Player1" -> {
"Pet1" -> {"Price" = 10,
           "Count" = 2}
--]]
}

Here is my code for the function:

local function createMultKeys(dictionary, keyValueList)
	if #keyValueList < 2 then return end
	
	if #keyValueList > 2 then		
		newTable = nil
		print(keyValueList[1])
		print(#keyValueList)
		if newTable == nil then
			dictionary[keyValueList[1]] = keyValueList[2]
		else
			newTable = keyValueList[2]
		end
		
		newTable = dictionary[keyValueList[1]]
		print(newTable)
		table.remove(keyValueList, 1)
		createMultKeys(newTable, keyValueList)
	else
		if newTable == nil then
			dictionary[keyValueList[1]] = keyValueList[2]
		else
			--table.insert()
			dictionary = keyValueList[2]
		end
		print(newTable)
	end
end
local t = {}
createMultKeys(t, {"Robot1", {"P1"},{"Cool"}, 2})
print(t)

One problem that I don’t know how to fix is that it adds P1 inside Robot1 but adds “Cool” in a separate table not nested inside P1 which I don’t want. And it also does not add the last value which in this case is 2.
What it prints:
image

Also what should I put for my parameters to achieve this:

--[[
"Player1" -> {
 "Pet1" -> {"Price" = 10,
           "Count" = 2}
}
--]]

Any help is appreciated!!!

Also I think the problems are happening because newTable does not return the key but the value of the key which I don’t know how to fix

It’s hard to understand what you want to happen, could you give a example input and from that what output you want?

1 Like

I want this:

"Robot1" -> {
 "P1" -> {
   Price = 10
   Count = 2
 }
}
--]]

Something like that

From what input? what parameters go into function create_my_table(t, ...) to make that output?

1 Like

In the function createMultKeys, the first parameter is the dictionary to add the keys to.
The second parameter is the keyValueList in which the last index will have the value(example: 10), and the other index’s will have keys to add(example: “Player1”) The function will read the keys and value from start to finish in order and create a nested table from them(example: “Player1” → “Robot1” → 10)

So, trying to match the example output you just gave me. If I used this example input:

local new_table = {}
create_my_table(new_table, {"Robot1", "P1", "Price", 10})
print(new_table)

would produce this output?

"Robot1" -> {
 "P1" -> {
   Price = 10
   Count = 2
 }
}

If this is incorrect please post a matching example input and output.
My current thoughts are why not just set the new_table to a produced table directly. Your function doesn’t handle depth dynamically which would be the only advantage to making a function for such uses.

I am not sure what you mean by setting new_table to a produced table but I am getting an error:

--Workspace.SellOrders.SellScript:20: attempt to index string with 'P1'

This is line 20:

newTable = nil
print(keyValueList[1])
print(#keyValueList)
if newTable == nil then
	dictionary[keyValueList[1]] = keyValueList[2] -- Line 20
else
	newTable = keyValueList[2]
end

Also do you how to create a function which handles depth dynamically?
And also the reason I am creating this function is because I need to create nested tables in lots of places, which is very tiring for me

Is this what you are looking for?

local function InsertNestedKey(Dictionary, Keys, Values)
	for Index = 1,#Keys do
		Dictionary[Keys[Index]] = {}
		Dictionary = Dictionary[Keys[Index]]
	end
	for Key,Value in Values do
		Dictionary[Key] = Value
	end
end

local Example = {"JunkStuff1","JunkStuff2"}
InsertNestedKey(Example, {"Key","Nest","SecondNest"}, {["ItemPrice"] = 500, ["ItemRarity"] = "Common"})
print(Example)

image

Wow yes but do you know how to do the same thing with recursion for efficiency reasons

Even after writing that code myself it still took me a second read to understand how it worked. It’s probably possible but its 2am and I am a tried novice scripter, so no.

Ok! Going to give you solution tick though since you solved it in one reply!

1 Like