Can you change a variable in a table with a number of indexes?

This is pretty hard to explain. So lets say we have a table of indexes

local iTable = {"a", "b", "c"}

And we have a normal table

local table = {"a" = {"b" = {"c" = 0}}}

Can you somehow change the value of “c” like this using the index table?
In other words, can you somehow iterate over the table and index table and do this:

table["a"]["b"]["c"] = 1

Do you mean you want to replace ‘a’ with something else? [ for example]?

What i mean is i can do this

function playerModule.GetData(plr, indexTable)
	local data = playerModule.GetDataTable(plr)
	local result = data
	for i, v in pairs(indexTable) do
		result = result[v]
		if result == nil then
			error("No value exist for this index table:"..indexTable)
		end
	end
	return result
end

But how can you do this when setting a value in a table?

Yes you can but you must use the correct syntax for dictionaries, meaning that the key must be wrapped between square brackets

local table = {["a"] = {["b"] = {["c"] = 0}}}

instead of

local table = {"a" = {"b" = {"c" = 0}}}

just make sure that every index before the last one is not nil

Yeah, i know that. i didnt test the code in studio. What i really mean is instead of doing this:

table["a"]["b"]["c"] = 1

How do i make a function that takes in a table, a index table, and the value to set that to?
(It’d do the same as the code above but it would be more flexible)

1 Like

Misread your post :grimacing:

Something like this (ignore bad variable naming, I wrote this in a rush)

local itable = {"a", "b", "c"}

local function TableFunction(Table, pos)
	local newTable = {}
	if pos < #Table then
		newTable[Table[pos]] = TableFunction(Table, pos + 1)
	else
		newTable[Table[pos]] = 0 -- set to whatever value
	end

	return newTable
end

print(TableFunction(itable, 1))
local function setTableFromIndices(t, is, v) --table, table of indices, value.
	for i = 1, #is - 1 do
		t = t[is[i]]
	end
	t[is[#is]] = v
end

local t = {a = {b = {c = 0}}}
setTableFromIndices(t, {"a", "b", "c"}, math.pi)
print(t.a.b.c) --3.14...
2 Likes

I’m fairly sure this isn’t what they were looking for.

Np, i explained it confusingly (Im still checking out the functions so that’s why i havent given a solution yet)

don’t really understand what your asking but maybe some of this might help

local keys = {"a", "b", "c"}
local values = {0, 0, 0}
values[3] = 1

or

local t = {["a"] = 0, ["b"] = 0, ["c"] = 0}
t["c"] = 1

or

local iTable = {}
iTable["a"] = {}
iTable["a"]["b"] = {}

iTable["a"]["b"]["c"] = 0
print(iTable["a"]["b"]["c"])

iTable["a"]["b"]["c"] = 1
print(iTable["a"]["b"]["c"])

also here is a module for handling 2D arrays that might help

local module = {}
module.__index = module

module.New = function()
	local self = setmetatable({}, module)
	self.values = {}
	return self
end

module.Get = function(self, a, b)
	if self.values[a] == nil then return nil end
	return self.values[a][b]
end

module.Set = function(self, a, b, value)
	if value == nil then
		if self.values[a] == nil then return end
		self.values[a][b] = nil
		if next(self.values[a]) ~= nil then return end
		self.values[a] = nil
	else
		if self.values[a] == nil then self.values[a] = {} end
		self.values[a][b] = value
	end
end

return module

and can be used like this

local table2D = module.New()
table2D:Set("A", "B", 1)
print(table2D:Get("A", "B"))
2 Likes

And if all of the indices are entirely new

local t = {a = {b = {c = 0}}}
setTableFromIndices(t, {"l", "o", "l"}, math.pi)

it errors like this:

Workspace.Script:4: attempt to index nil with 'o'

Your code is really confusing so i dont know how to fix this myself. Could you maybe help me again with this? (I need this btw)

local function setTableFromIndices(t, is, v) --table, table of indices, value.
	for i = 1, #is - 1 do
		if not t[is[i]] then t[is[i]] = {} end
		t = t[is[i]]
	end
	t[is[#is]] = v
end

local t = {a = {b = {c = 0}}}
setTableFromIndices(t, {"l", "o", "l"}, math.pi)
print(t.l.o.l) --3.14...

I didn’t realize you wanted to support non-existent indices too.

1 Like