Table unpacking only returns first index

I’m trying to find some words in a table, add those words in a seperate table, then unpack it for string concatenate, but it only returns first index.

local text = "hi this is a test that contains some words that are in a cool table"
local words = {
	"test",
	"cool",
	"words",
	"some"
}
local splitedtext = text:split(" ")
local found = {}
for _,v in splitedtext do
	if table.find(words, v) then 
		table.insert(found, v)
	end
	if v:find("contain") then
		table.insert(found, v)
	end
end
print(found) -- Returns all found words
print("bannable words "..table.unpack(found)) -- Only returns test

image

You have to write your print statement like this:

print("bannable words", table.unpack(found))
--                    ^ automatically puts a space

Concatenation will only concatenate the first element from a tuple

What the very cool boy above said is the reason, just to add to that, you perhaps meant to use table.concat() to concatenate the table into a unified string.

local separator = ""
print("Content: " .. table.concat(table.pack("element1", "element2"), separator))
1 Like

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