How I can write to dictionary, if Key is array?

I have dictionary, which use arrays for keys. As I know, {3, 5} == {3, 5} won’t be true bc this’s 2 diffirent tables, and due to this I have question - how I can replace Value in this situation, because writing with identical table should create new key-value pair?

local Dictionary = {
    [{3,5}] = "Flame"
}
Dictionary[{3,5}] = "Star"
print(Dictionary[{3,5}]) --output is nil, because there's again new table

Yeah as you said it’s pretty much impossible only way is to store the reference to the table in a variable which is not what you want I believe.

local key = {3,5}
local Dictionary = {
    [key] = "Flame"
}
print(Dictionary[key])
--The only way with the above method

You should either combine the key into one string or use a 2d array like dictionary

--2d array, kind of
Dictionary[3] = {}
Dictionary[3][5] = "Flame"
print(Dictionary[3][5])
--string combination key
Dictionary["3,5"] = "Flame"
1 Like

why not change the key with the umm idk whats it called but with the item on the right side of the “=” sign? unless both are a array.

the problem of this way is there may exist many arrays with same value:
{3,5} = “Flame”
{4,7} = “Star”
{1,1} = “Flame”
{8,1} = “Flame”

ah yes i forgot.

It’s still referred to as a value.

oh thanks!

local function IndexTableKeyedTable(Table, ...)
	local Arguments = {...}
	for Key, Value1 in pairs(Table) do
		if type(Key) ~= "table" then continue end --Ignore non-table keys.
		for Index, Value2 in ipairs(Key) do
			if Value2 ~= Arguments[Index] then break end
			if Index == #Key then return Value1 end
		end
	end
end

local Table = {
	[{3, 5}] = "Flame",
	[{4, 7}] = "Star",
	[{1, 1}] = "Flame",
	[{8, 1}] = "Flame"
}

local Value = IndexTableKeyedTable(Table, 4, 7)
print(Value) --Star

You could define a custom function (like in the above) for querying tables with table indices.

The first code what will the ouput print out may I ask??

It would print out “Flame” as expected. You can test this yourself by running the code through an online Lua compiler or Roblox Studio’s command bar.

2 Likes