Check if variable is an array

Hello, quick question.
How do I tell if a variable is an array?
I tried using typeof(), but it can only check tables and not arrays.

if table.find(array, variable) then
    print("is in array")
end

…I meant, an array, not in array.

Arrays are limited to using numbers as indices, which differentiates them from dictionaries.

1 Like

Why don’t you print typeof() both arrays and see what they are?

1 Like

90% sure arrays basically do not exist in Lua. Arrays have a fixed length and cannot be modified, and all the items in it are the same data type. I don’t think that could be created in Lua, except if you modified the __newindex metamethod of the table. Then, you could do:

array = {}
array.__newindex = function(tbl, key, value)
    warn("new indexes are not allowed.")
end

if type(array.__newindex) == "function" then
    print("is an array")
end

I think that’d be the closest you can get.

Tried this, and… it’s considered a table?

Arrays and dictionaries are subcategories of tables.

1 Like

The wiki didn’t explain much to me.
Well then. Thank you!

Array equivalent in Lua are tables without dictionary keys.

local array_tbl = {
"value",
100,
false
}

compared to dictionaries:

local dictionary_tbl = {
some_key = some_value
}

or

local dictionary_tbl = {
["some_key"] = some_value
}

Let’s say you had a function and you send two types of tables through and you have a for loop but you must run the right loop for the type of table. You can check if a table is an array by simply:

local function is_array(t)

  	for k, _ in pairs(t) do

		if type(k) ~= "number" then

			return false

		end

	end

	return true
end
1 Like

yeah my bad i got confused with a different concept

but the only problem is arrays have a fixed length and no key. So Lua arrays aren’t really “proper” arrays. For example dictionaries might be more classed as associative arrays.

Sorry, forgot to add the context of this message.
Arrays in Lua always have an index-value pair, whether you see the index or not. Dictionaries have a key-value pair, where the key is a string and not a number.

local function isArray(t)
    if type(t) ~= "table" then return false end
    if #t == 0 then return true end
    for i, _ in t do
        return type(i) == "number"
    end
end

You have basically just reiterated what @LucidBits said above. If you were attempting to solve the topic, please reply to that and not to me.

Yeah, that’s true, but if you try to loop through a dictionary using ipairs it won’t work because the indexes of a dictionary aren’t integer based.

I just want to point out that this function will return true for mixed tables and even pure dictionary-style tables

local function isArray(t)
	if type(t) ~= "table" then return false end
	if #t == 0 then return true end
	for i, _ in t do
		return type(i) == "number"
	end
end

print("Is dictionary array:", isArray({ 
	KeyA = 10,
	KeyB = 20
})) -- "Is dictionary array: true"

print("Is mixed table array:", isArray({ 
	[1] = 10,
	[2] = 20,
	KeyA = 30
})) -- "Is mixed table array: true"

Oh yeah. I didn’t think about that cuz I tend to iterate using next instead of those.

But it still poses the question whether you can get a true array in Lua.

1 Like

I’m pretty sure @LucidBits’s method is the only real way to determine if a table is a pure array or not. I’m curious why one would need to determine whether a table is an array or not though. I don’t think I’ve been in a situation where I needed to determine whether a table was an array or not at runtime.

1 Like

Hey no problem! And I use next too when I need to check if a table is empty. About tables being a true array: I feel like it’s fine as long as you can access a table like an array. An array can be accessed the same way an integer based table can be. someArray[0] is the same as someTable[1]

The main thing that kills it for tables being a true array is that it’s flexible. It can grow or shrink but if you wanted it to be a set size you could do that too.

I like how it also doesn’t entirely matter if it is a true array, since it could just be treated as an array if it is a table. I think my method using the __newindex metamethod may be very close to array-like behaviour, the new key could always be typechecked. It’s a bit like OOP; you cannot do it the same way as other languages, but you can produce an almost identical effect.

1 Like