How to know if something is a dictionnary

Hello, I would like to know if it’s possible with a function to know if something is a dictionnary or not. I would like to make a selection in order to put it on attributes, and lists and dictionnaries aren’t supported types. Thanks in advance.

2 Likes

I think table.find can find in a dictionary too? (I don’t remember tho)

I want to know if something is a dictionary, not if something is in a dictionary

1 Like

please do some searching before posting.

that post is a way to get a more precise type so if you dont need that you could probably get away with doing something like this

-- updated this because the original ternary operation wasnt sufficient
function checkTableType(t)
	for i in t do
		return typeof(i) == "number" and "array" or "dictionary"
	end

	return "empty"
end
1 Like

OOOOOOOHHHHHH SORRRY IM BLIND :money_mouth_face::money_mouth_face::money_mouth_face:
Just do

if dictionary[smth]==nil then
--Not in dictionary 😡😡
end

All tables are the same type. How you use them defines what they are.
List (array): sequential integer keys starting from 1, no gaps.
Dictionary (map): non-sequential keys or non-integer keys.

Version 1
function tableType(t)
	if typeof(t) ~= "table" then return nil end
	for k in pairs(t) do
		if typeof(k) ~= "number" or k < 1 or k % 1 ~= 0 then
			return "dictionary"
		end
	end
	local i = 0
	for _ in pairs(t) do
		i += 1
		if t[i] == nil then
			return "dictionary"
		end
	end
	return "list"
end

--8 tests
local tests = {
	{"a", "b", "c"},
	{one = 1, two = 2},
	{},
	{[1] = "x", [3] = "y"},
	{true, false, [4] = true},
	"hello",
	123,
	true
}

for _, v in ipairs(tests) do
	print(tableType(v))
end
version 2
function tableType(t)
	if typeof(t) ~= "table" then return end
	local n = 0
	for k in pairs(t) do
		if typeof(k) ~= "number" or k % 1 ~= 0 or k < 1 then return "dictionary" end
		n += 1
	end
	for i = 1, n do
		if t[i] == nil then return "dictionary" end
	end
	return "list"
end

--5 tests
local tests = {
	{"a", "b"},
	{a = 1},
	{[1] = "x", [3] = "y"},
	{},
	"not a table"
}

for _, v in ipairs(tests) do
	print(tableType(v))
end
1 Like

i dont believe this would work for making sure the # operator would work though because the length operator cant count arrays with missing indexes, this function should work for that purpose

-- you might be able to optimize this, i just threw this together as an example
function checkTableType2(t)
	local t_type = "empty"
	local lastIndex = 0
	local consistent = true
	
	for i in t do
		consistent = typeof(i) == "number" and lastIndex + 1 == i or false
		
		if consistent then
			t_type = "array"
			lastIndex = i
		else
			t_type = "dictionary"
			break
		end
	end
	
	return t_type
end

you can make it even more stuipid simple

if table[1]==nil then
--its a dictionary
end

Thanks you, you’ re right :smile: :ok_hand::ok_hand:

1 Like

this would not work for the purpose of checking if the length operator would work since the length operator cant count arrays with missing indexes, also this version doesnt account for string indexes and number indexes in the same table

what the point of doing that?
This table will not be an array anyways

i dont get your point, i gave two versions with different purposes, the first to do a basic check for the first indexes type, the second (which you replied to) for the purpose of checking if the length operator can be used properly

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.