lua-users home
lua-l archive

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


Hello,

Is there any builtin way to output table content? Couldn't find in docs, maybe I'm blind...
Also, I would enjoy some clues about the reasoning for tostring(t) not doing that by default (and print(t) not writing that). Probably getting the memory address is useful in specific occasions, so there should certainly be a method for this in library "table". But the table content is certainly what we need in most cases, esp. during developpment/testing/debug phases, so that this should be the easiest output to get. Or am I missing something

If the answer to the first question is "no", then take this post as a feature request for the "table" library. Actually, I guess 2 table output formats may be wished (called below "view" and "treeview); and matching pseudo-print funcs to avoid the burden of writing eg:
   print(myTable:view())
but instead directly:
   myTable.show()

Below a (superficially tested) example of what I mean. (The func names don't pretend to be best.) Still, I guess the default output for a table should be a (flat) view of its content. Even if big (the programmer knows it).

=======================================================
-- table output

-- basic tools ---
require "coroutine"--, "debug", "file", "io", "math", "string",
require "table"
require "os" ; exit=os.exit
p=print
stringmult = function(s, n)
	local ss = ""
	for i=1,n do
		ss = ss..s
	end 
	return ss
end

--- a table to hold output funcs ---
T = {
	-- view separators -- LS:line sep -- IS:item sep -- KS=key sep
	LS = '\n', IS = "  ", KS = ":",
	-- default indentation string
	indent = "   ",
	view = function(t)
		local index, item_view, item_views, items_view
		item_views = {} ; index = 0
		for key,item in pairs(t) do
			index = index + 1
			-- case table: recursion
			if type(item) == "table" then
				item_view = key..T.KS..T.view(item)
			-- else: simple view
			else
				item_view = key..T.KS..tostring(item)
			end
			item_views[index] = item_view
		end
		items_view = table.concat(item_views, T.IS)
		return "{"..items_view.."}"
	end,
	show = function(t)
		print(T.view(t))
	end,
	treeview = function(t, level, indent)
		local index, item_view, item_views, items_view, offset, IS
		-- indentation offset -- item sep
		level = level or 1
		indent = indent or T.indent
		offset = stringmult(indent,level)
		IS = T.LS..offset
		-- item views
		item_views = {} ; index = 0
		for key,item in pairs(t) do
			index = index + 1
			-- case table: recursion
			if type(item) == "table" then
				item_view = key..T.KS..T.treeview(item, level+1, indent)
			-- else: simple view
			else
				item_view = key..T.KS..tostring(item)
			end
			item_views[index] = item_view
		end
		items_view = IS..table.concat(item_views, IS)
		header = level==1 and "table:" or ""
		return header..items_view
	end,
	showtree = function(t)
		print(T.treeview(t))
	end,
	test = function()
		circle = {radius=9, position={x=3,y=2,z=1}, color={r=33,g=99,b=66}, code={1,{2,3}}, drawfunc=function() print("foo") end}
		print ("=== test table:")
		print ("    ".."circle = {radius=9, position={x=3,y=2,z=1}, color={r=33,g=99,b=66}, code={1,{2,3}}, drawfunc=Function() print('foo') end)}")
		print ()
		T.show(circle)
		T.showtree(circle)
	end,
}

--- test ---
T.test()
=======================================================


Denis
------
la vita e estrany