lua-users home
lua-l archive

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


On Tue, 11 Oct 2011, Xavier Wang wrote:

hi list :)

In Lua, to print a table is some how difficult: if you print it
direct, a useless string of table: address will print, use
table.concat, you can print a sequence easily:

local t = {1,2,3,4,5}
print(t) --> table: xxxxxxxx
print(table.concat(t, ",")) --> 1,2,3,4,5

but you can not use this way to print a hash table:

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

some, maybe table.concat can accept a second sep:
print(table.concat(t, ':', ',') --> a:1,b:2,c:3
	Take a look at fullconcat() from Dado's table.extra module:

http://www.ccpa.puc-rio.br/software/dado/luadoc/modules/table.extra.html#fullconcat

	For your example:

fullconcat = require"table.extra".fullconcat
t = { a = 1, b = 2, c = 3 }
=fullconcat(t)
a=1,b=2,c=3
=fullconcat(t, ':', ',')
a:1,b:2,c:3

	Regards,
		Tomás