Module:Sandbox/Wnt/DisplayLuaTableContents

local p={}
local debuglog = ""
function p.flat(t)
	local todo = {t}
	local pointer = 1
	local flat = {}
	local marked = {}
	marked[t] = true
    while todo[pointer] do
		for i, j in pairs(todo[pointer]) do
		    if ((type(j) == table) and (not marked[j])) then
		    	debuglog = debuglog .. "marking" .. tostring(j)
		    	marked[j] = true
		    	table.insert(todo, j)
		    end
		    if (not flat[i]) then
		    	debuglog = debuglog .. "new item" .. tostring(i)
	            flat[i] = {j = true}
	        else
	            if (not flat[i][j]) then
	            	flat[i][j] = true
	            end
	        end
	    end
	    pointer = pointer + 1
    end
    debuglog = debuglog .. tostring(#flat)
    return flat
end

function p.tostring(t, delimiter)
	if not delimiter then delimiter = "" end
	local flat = p.flat(t)
	local outarray = {}
	for i, j in pairs(flat) do
		debuglog = debuglog .. tostring(i) .. tostring(j)
		local kk = {}
		for k in pairs(j) do
			table.insert(kk, k)
		end
	    table.insert(outarray, table.concat(kk, delimiter))
	end
	return table.concat(outarray, delimiter)
end

function p.demo(frame)
	local a = {j=1, 2, 3}
	table.insert(a,a)
	-- a[a] = a -- (this may be just too crazy; getting "attempt to call a table value")
	return p.tostring(a)..debuglog
end
	
function p.main(frame)
   local args=frame.args
   local pargs=frame.getParent(frame)
   local dispvar="mw"
   if pargs then dispvar=pargs.dispvar or pargs[1] or dispvar end
   if args then dispvar=args.dispvar or args[1] or dispvar end
   local base=_G[dispvar]
   local novar, containssomething
   if base then else base={};novar=yes end
   local output="''Contents of the array '''''" .. tostring(dispvar) .. "''':"
   if novar then output = output .. "<u>nil</u>." end
   local ns=0
   local count=0
   for i,j in pairs(base) do
      containssomething="yes"
      output=output .. "<br />mw." .. tostring (i) .. ":" .. tostring(j)
      if tostring(j)~= "table" then j={} end
   for a,b in pairs(j) do
      output=output .. "<br />mw." .. tostring(i) .. "." .. tostring(a) .. ":" .. tostring(b)
      if tostring(b)~="table" then b={} end
   for x,y in pairs(b) do
      output=output .. "<br />mw." .. tostring(i) .. "." .. tostring(a) .. "." .. tostring(x) .. ":" .. tostring(y)
      if tostring(y)~="table" then y={} end
      for w,z in pairs(y) do
          output=output ..  "<br />-->" .. tostring(w) .. ":" .. tostring(z)
             if tostring(z)~="table" then z={} end
             for u,v in pairs(z) do
                output=output .. "<br />----->" .. tostring(u) .. ":" .. tostring(v)
             end
      end
   end
   end
   end
   --local getc=mw.title.getcontent
   --output=output .. "<br />P.S.: mw.title.getcontent is:" .. tostring(getc)
   --if tostring(getc) == "function" then output = output .. "<br />" .. tostring (getc(mw.title)) end
   if containssomething then else output = output .. "<br />''No pairs(" .. tostring(dispvar) .. ") found!''" end
   return output
   
end
 
return p

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.