Checking Tables

How can I check if somethins is part of a table. For example lets say I have something called


local secrets = {"dog", "cat"}

Then I will make something called PetChosen

PetChosen for example equals to “dog”

How can I say if Pet Chosen is part of the table print “SECRET”

if table.find(secrets,PetChosen) then
—code here
end

1 Like

Thank You :smiley: . I just realised that I can use that.

If you want to learn some interesting stuff with loops and tables, here’s another method using a simple algorithm called “Linear Search.” It just uses a for loop to check for whatever element in a table you’re looking for, and then stop when it’s found. The nice thing is that you can customize this however you’d like through extra programming unlike built-in functions.

Here:

local secrets = {"dog", "cat"}

local function linearSearch(table, toFind)
    for i, v in pairs(table) do
        if v == toFind then
            print("Found!")
            return true
        end
    end
    print("Not Found.")
    return false
end

linearSearch(secrets, "cat") -- Calls the function

Let’s break the function down a bit.

local function linearSearch(table, toFind)

This line of code creates the local function “linearSearch” with the parameters (inputs) - table being the table to search through, and toFind being the element to search for in the table.

for i, v in pairs(secrets) do

This will go from the first element to the last element of the table secret, and run any code “nested” within the loop (that’s inside it). This uses Lua’s in pairs for loop, where it will take the table you want to iterate through as the parameter, which in this case is your table secrets. i represents the place of the element it’s currently looking at. To clarify, in your table, "dog" is the first element (i = 1), and "cat" is then the second (i = 2). And also, v represents the actual element’s value, not which place it’s in. So in i = 1, or the first element, there is the value "dog", and in i = 2, or the second element, there is the value "cat". Extra Note: You can rename i and v to whatever you’d like - so in for element, secret in pairs(secrets) do, element is the same as the previous i, and secret is the same as what v was.

        if v == toFind then
            print("Found!")
            return true
        end

Here, this is “nested” within the loop discussed above. So every time that the loop checks the next element in the table, it will check if the current element’s value is equal to the element you’re trying to find, which you will need to input when calling the function (discussed below). If it’s found, it will return true from the function, saying that it was found, which can be used in your own other code if needed.

        end
    end
    print("Not Found.")
    return false
end

So here, the two end’s are of course for the function, loop, and conditional statement discussed above. Now in this function, true represents found, and false represents not found. So why is it returning false in this code (and printing “Not Found.”)? Well, in the earlier code, we first loop through all of the elements of the table, and if we find it, we return true - remember that return will stop the function completely where it’s at, and return the value given. So in that case, since if the earlier code doesn’t return true and stop the function, that means that toFind wasn’t found in the table given! So if the earlier code doesn’t find the value in the table, it will move on to this code and basically say, “Okay so if it wasn’t found earlier, then I have to return that as false.” Thus, it’s like a back-up scenario if the earlier code doesn’t say it was found. I hope that made sense aha.

linearSearch(secrets, "cat") -- Calls the function

This final line of code will “call” the function, meaning that it will run the functions code, with the parameters (inputs) secrets, and "cat". If we look at the earlier code, we can see that our function takes in the parameters, firstly, "table", and secondly, "toFind". So when we place our table secrets in the first spot of the parameters, and what we’re trying to find (in this say let’s say it’s "cat") in the second parameter, this will sort of “take the place” of those parameters. That’s really the “beauty” of functions - that if designed properly, they can take in any input required and can be fundamentally “re-used.” Like in this example, you can put any table and value to find in the parameters when calling the function and it will work! Examples:

linearSearch(secrets, "dog") --> Will return true and print "Found!"
linearSearch(secrets, "game") --> Will return false and print "Not Found."
linearSearch(secrets, "animal") --> Will return false and print "Not Found."

I hope this helped, and let me know if there’s any questions!

Regards,
MJT Free Time (Matthew)

1 Like

That function is literally the same thing as table.find. You’re just re-inventing the wheel, also, a key index is generally leagues faster than a for loop.

Yes, exactly. What I’m trying to show OP is how this function works on the inside. And the thing is that the approach I explained that you called “re-inventing the wheel” is much more customizable to different use cases. For example, if you wanted to find every occurance of a string in a given table and then insert all of them into another table, how would you do that with table.find()?

I don’t get what you’re referring to in this statement. The Roblox Developer API Reference states at the bottom of this page that it performs a linear search, which I just presented in my earlier post. The only slight performance boost I can think of is that table.find() likely is a library from the C language - but that’s negligible for a substantial amount of applications.

And don’t take any of this the wrong way. I wasn’t trying to replace your solution by any means, but rather show OP a more interesting/in depth way of doing this same operation. I said in one of the two above quotes, “If you want to learn some interesting stuff with loops and tables, here’s another method…” I did not have any intentions of sabotaging your post, just to be clear. Thanks for the feedback though.

1 Like