0

Take a look at the for loop and the word part of it specifically.

local words = { 
    "One - Test", 
    "Two - Test", 
    "Three - Test"
}

local find = "Test"

local function getWord(partialName)
    partialName = partialName:lower()
    for _,word in ipairs(words) do

        if string.find(word:lower(),partialName) then
            print(word)
        end
    end
end

getWord(find)
Output:

One - Test
Two - Test
Three - Test

I'm trying to store everything that gets outputted to other variables. print(word) outputs what you see above, but how can I only get the One - Test result of it and store it to another variable? I've tried using print(word[1]) to test it out, but it didn't work and outputted nil.

nil (x3)  -  Client - Local Script:14

Now how can I fix that? Very much appreciated!

4
  • Do you want to get only One, or One - Test? If One, you need to split the string and return the first part, if One - Test, you can get it by words[1]. Commented Oct 4, 2021 at 12:28
  • @pavelsaman Let's say that I have a huge table and used this script to locate "One - Test" using the "find" variable, I would like to store the outputs in order to other variables and "word[1]" doesn't help unfortunately. Also words[1] is only for when I know where "One - Test" is, but let's say that I didn't. Commented Oct 4, 2021 at 12:32
  • You can create a lookup table like so huge_table = { ["One - Test"]=true, ["Two - Test"]=true }, then searching for some key would be just huge_table["One - Test"]. Commented Oct 4, 2021 at 12:41
  • One - Test is not a word. You should use more suitable names. Same for getWord. A function named getWord should return a single word. Commented Oct 4, 2021 at 12:47

1 Answer 1

1

Instead of printing your result you can simply put every result in a table.

local words = { 
    "One - Test", 
    "Two - Test", 
    "Three - Test"
}
   
local find = "Test"

local function getWord(partialName)
    partialName = partialName:lower()
    local output = {}
    for _,word in ipairs(words) do 
        if string.find(word:lower(),partialName) then
            table.insert(output, word)
        end
    end
    return output
end

print(table.concat(getWord(find), "\n"))
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much and yes, I will choose more suitable names later on, this was just for testing!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.