Checking array value's position in a table

Hello developers, I have been trying to check if the value “fire” in the table is in the 5 index, but I do not know how to properly check it. Maybe there is a way to check if the third argument of “table.find” can be assigned to a variable? I would be happy if anybody could help.

(P.S. the if statement is not working if your still confused…)

code:

local array = {"fire", "hotdog", "meat", "fire", "hotdog", "fire"}

local fire = table.find(array, "fire", 5)

if fire[index] == 5 then
	print("fire is at index 5")
else
	print("fire is not at index 5")
end

To check if fire is at index 5:

print(array[5] == "fire")

To check if fire is at or after index 5:

print(table.find(array, "fire", 5) ~= nil)

To check all the indexes within a table that have the value fire:

local function getIndexes(t: {any}, val: any): {number}
	local results = {}
	local i = 1 --in lua arrays start from 1(I hate it)
	while true do
		local index = table.find(t, val, i)
		if not index then break end
		table.insert(results, index)
		i = index+1
	end
	return results
end

print(getIndexes(array, "fire")) --{1, 4, 6}

So to check if fire is at index 5 I don’t need to use “table.find”? You only need to use “table.find” while trying to check if fire is at or after index 5?

table.find is only useful as long you don’t know the index(because it searches for it). But in your case you know index is 5, so you can directly index the table and check if the value matches the expected one(in this case fire), if the direct comparison(==) is valid, it will return true, else false. This can be used inside logic statements to perform actions of your choice.

Oh, got it! Also, could you further explain what is this mess in the functions argument section:

(t: {[number]: any}, val: any)

Also, my brain hurts from this one too:

(t, val, i) -- this is from the "local index = table.find..."

Thanks.

This is type checking, it tells the code to expect a table with number indexes as the first argument. A table with number indexes(instead of string indexes) is called an array. The functions of the table library can only be used on arrays, so it tells the code that t must be an array. any means any value, meaning that each key can store any value, such as a bool value, number, string, Instance, etc.

Typechecking doesn’t stop code from erroring or change its behaviour, it just warns the programmer about a potential error by redlining code(basically it helps error prevention and makes it easier to understand function behaviour).

This basically performs a repeated search in an array for the same value, for example: table.find(array, "fire", n) where n starts from 1(the default starting index for arrays) and is increased to the index we find the next location of the value in(since we search from the start) +1(so we ignore the value we found in the last iteration and avoid an infinite loop). For your case, the behaviour should be:

--[[
first iteration we find fire at 1, next i = 1+1 = 2
second iteration we find fire at 4, next i = 4+1 = 5
third iteration we find fire at 6, next i = 6+1 = 7
fourth iteration we didn't find any more fire, we return {1, 4, 6}
]]
The functions of the `table` library can only be used on arrays, 
so it tells the code that `t` must be an array.

Quick question, when you mean “the functions of the table”, do you mean these ones:
table.find, table.create, table.insert.

1 Like

Yeah, also the reason I split terminology in table and array is because in lua we tend to use arrays and dictionaries a lot. An array is basically a list of elements that are often of the same type(although not a requirement) and has number keys that determine the position of said elements within it. A dictionary on the other hand isn’t a list, its more of a data structure itself, for example something like:

local data = {
	coins = 25,
	diamond = 50,
	hasVIP = true
}

is a dictionary, because the keys are strings(coins, diamonds, hasVIP). Dictionaries are often used for storing player data in datastores.

By using the word table I mean the more general abstraction of dictionaries and arrays, where the key can be of any type(not only numbers and strings). However we avoid using weird key types because it can interfere with data saving or make it hard to JSON encode/decode said data.

The keys is the dictionary that you provided are strings but they do not have the “”. But I guess you can do that because you are technically not setting a variable. You cannot set a variable in a dictionary right?

1 Like

Yeah as long the key doesn’t have spaces lua allows you to do that. If it has spaces however you must do it the following way:

local data = {
	["a key with spaces"] = 1,
	aKeyWithoutSpaces = 2
}
--to index key with spaces:
print(data["a key with spaces"])
--to index key without spaces:
print(data.aKeyWithoutSpaces)

you can index and define a key without spaces in the same way as one with spaces, but not vice-versa.

I see you have square brackets around the key with spaces but not around the one without spaces, why is that? I heard that nor you need brackets around strings with spaces nor around strings without them. So how is it done…

For indexing tables that have string keys with spaces you need the brackets. But for just mentioning strings, for example:

print("Hello World!")

you don’t.

Oh, thanks for clarification, I can see that overall, this topic is too advanced for me. I can only understand the main stuff, but anyway thanks for helping!

My last question, you said this:

local i = 1 --in lua arrays start from 1(I hate it)

Do you mean that you know more programming languages? I know that other programming languages use 0 index thingy, but how can you hate it, unless you know a second programming language? Anyway, just curous XD.

Math are easier when indexes start from 0, adding +1 offsets tend to complicate the equations needed for certain algorithms to function.

1 Like

Oh, thanks for the explaining! You helped me a lot, here is your 400th solution!
image

1 Like

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