What is table.concat() actually do?

Hello everyone!

I am a bit confused, and thats the concat, so what is that actually do? I am new in tables, and I want learn everything what I need. I never was so intrested in tables. I worked with metatable but not with tables, anyways.

So can somoene explain me everything of that table.concat()?

How you explain me it:

  1. What does it do?
  2. What is the parameter exactly of it - do I need to use 1 or the second and when?
  3. Give me a script example.
  4. Give me a example when I have to use it.

Important : Take your time with your explanation Thanks!
Make sure you have everything also when ,I have to use it!!"

5 Likes

It allows you to create a string from a table’s element, for example, for a table beautify function:

local t = {1, 2, 3, 4, 5}
print(table.concat(t, ', ')) --1, 2, 3, 4, 5

Here, the second parameter is the separator between each element. By default, it’s nothing, so all the element will be conjoined (for our example, it’d be 12345). There’s also an optional 3rd and 4th parameters to signify the start and end index:

print(table.concat(t, ', ', 2, 4)) --2, 3, 4

The wonderful thing is it automatically doesn’t put the separator after the last character.

One thing is that if concatenation can’t be done directly with a table element (e.g. a bool), then the function will error:

local t2 = {1, 2, true, 3}
print(table.concat(t, ', ')) --error
27 Likes

so for what do I need that, that doesnt seem useful for me

2 Likes

just set a " , " or " /n " isnt really useful or am I wrong?

2 Likes

So, again I can just, and really just, let a table look more beautiful and nothing more?

Like I want create a rpg game and sometimes I want add something with text and I will not need in that case concat?

So just for make things beautiful I will need the concat function

It has it’s uses especially in user input which is different all the time. table concatenation manifests itself in whatever you’re doing, you really need to start experimenting around to find it’s uses

3 Likes

table.concat(table, suffix) is basically short for

local function concat(table, suffix)
  local new = ""
  for i=1,#table do
    new = new..table[i]..suffix
  end
  return new
end
2 Likes

Oh okey thats nice but I dont think I will need concat

3 Likes

its useful for making a loading screens … for example

1 Like

Okey wait now I am confused loadscreens?

Take this for example, it doesnt use table concat but it uses pretty much the exact same thing (doesnt use suffix)

Not exactly. The .. operator could be overloaded, table.concat only concatenates strings and numbers.
Internally, IIRC table.concat creates a new string at one function call.

Here’s a closer implementation for table.concate

static void addfield (lua_State *L, luaL_Buffer *b, int i) {
  lua_rawgeti(L, 1, i);
  if (!lua_isstring(L, -1))
    luaL_error(L, "invalid value (%s) at index %d in table for "
                  LUA_QL("concat"), luaL_typename(L, -1), i);
    luaL_addvalue(b);
}


static int tconcat (lua_State *L) {
  luaL_Buffer b;
  size_t lsep;
  int i, last;
  const char *sep = luaL_optlstring(L, 2, "", &lsep);
  luaL_checktype(L, 1, LUA_TTABLE);
  i = luaL_optint(L, 3, 1);
  last = luaL_opt(L, luaL_checkint, 4, luaL_getn(L, 1));
  luaL_buffinit(L, &b);
  for (; i < last; i++) {
    addfield(L, &b, i);
    luaL_addlstring(&b, sep, lsep);
  }
  if (i == last)  /* add last value (if interval was not empty) */
    addfield(L, &b, i);
  luaL_pushresult(&b);
  return 1;
1 Like

what do you mean overloaded? you cant index a boolean with a string

But I cant see table.concat. as you said.

So concat is useless for me and just in moments useful if I work with strings and numbers

I use it mostly for admin commands to concat arguments into a string.

2 Likes

It’s EXTREMELY useful in cases of putting together a table in the form of a list to show to a player. Otherwise your code would be much longer.

2 Likes

What the hell is this code? Why is it so complex?

1 Like

It is C++ code from the Luau source code.

1 Like