lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> local t = {a:1, b:2, c:3}
> print(table.concat(t, ':', ',') --> a:1,b:2,c:3

Lua use = in table constructors and not :  so your example should be really:

  local t = {a=1, b=2, c=3}
  print(table.concat(t, ',')) --> empty line
  print(table.concat(t, ',', '=') --> a=1,b=2,c=3

Also note that I have permuted the order of the separators, to avoid
the second argument to behave differently whether or not a third one
is present (there is a similar problem in table.insert)

> What about: {a={b={c={1, 2, 3, 4}}}}?
It won't work, of course.

And the result of that code won't be valid Lua syntax :
  local t = {1, 2, [1.5]=3, ["key with space"]=4}
  print(table.concat(t, ',', '=') --> 1=1,2=2,1.5=4,key with space=4

On my opinion, a serious table serialization routine like my
DataDumper [1][2] should be present in every Lua environment, because
it is so common and useful to print tables in a readable way. The
proposal of Xavier Wang may be of interest in some cases, but can in
no way replace a full recursive serialization function.

[1] http://lua-users.org/wiki/DataDumper
[2] https://gist.github.com/1255382