What's the best way to get everything in a table into a string? (SOLVED)

let’s imagine i have a table which pprints as:

{
1 = i,
2 = ,
3 = l,
4 = o,
5 = v,
6 = e,
7 = ,
8 = d,
9 = e,
10 = f,
11 = o,
12 = l,
13 = d,
14 = !,
}

And i want to make self.textinput = “i love defold!”

What would be the most efficient way to do that? I have very rudimentary knowledge of strings and tables.

1 Like

I think the easiest way is using table.concat, if your test data is just strings at least. Something like this:

❯ cat test.lua
a = { "i", " ", "l", "o", "v", "e", " ", "d", "e", "f", "o", "l", "d", "!" }
print(table.concat(a))

❯ lua test.lua
i love defold!
5 Likes

This should work:

local string = ""
for k, v in ipairs(yourTable) do
	string = string..v
end
print(string)

Edit: Yeah, the method above is certainly simpler =P

2 Likes

Sven for the win!! Thanks dude.

1 Like

kingklear, thanks also for your input.

2 Likes

subquestion: is this the sort of question that’s better in the slack channel? It seems like a shame to make whole thread.

1 Like

I think it’s totally fine on the forums, feels like a question more people would benefit from! :slight_smile:

3 Likes