Lua 4 "n" property of tables -
in lua 4, many tables have "n" property tracks number of items inside table.
do tables have property? can overridden?
i ask, because i'm trying develop routine prints of table's elements recursively in valid lua syntax, , want know if it's safe filter "n" items out of result?
thanks.
[edit]
here's script:
-- thoughtdump v1.4.0 -- updated: 2017/07/25 -- ***************** -- created thought (http://hw2.tproc.org) -- updated mikali -- description -- *********** -- parses globals table , __tdprints contents "hw2.log". -- can used parse (i.e., pretty-print) generic tables in cases. -- note: functions & variables must declared in order parsed. -- otherwise, ignored. -- note: if parsing table other globals table, __tdprinted table -- values may in different order written. values -- numerical indices moved "top" of table, followed values -- string indices, followed tables. functions appear in different -- locations, depending on whether indexed using number or string. -- note: despite fact nil values cannot stored in tables, -- still handled. -- note: though functions may referenced within tables, function -- parsed correctly if indexed using string same -- name of function. __tdoutputstring = "" function __tdparse(name, value, level, verbose, numbers, collapse) if ((name == "__tdparse") or (name == "__tdsorthash") or (name == "__tdprint") or (name == "__tdprintglobals()") or (name == "__tdoutputstring")) return end local element = nil local valtype = type(value) local namtype = type(name) local prelevel = "" if (collapse == 0) = 1, level prelevel = prelevel .. "\t" end end local comlevel = "" if (level ~= 0) comlevel = "," end if ((valtype == "function") or (valtype == "userdata")) if (namtype == "string") element = prelevel .. name .. " = " .. name .. comlevel elseif (numbers == 1) element = prelevel .. "[" .. name .. "] = " .. name .. comlevel else element = prelevel .. name .. comlevel end elseif (valtype == "string") if (namtype == "string") element = prelevel .. name .. " = \"" .. value .. "\"" .. comlevel elseif (numbers == 1) element = prelevel .. "[" .. name .. "] = \"" .. value .. "\"" .. comlevel else element = prelevel .. "\"" .. value .. "\"" .. comlevel end elseif (valtype == "number") if (namtype == "string") element = prelevel .. name .. " = " .. value .. comlevel elseif (numbers == 1) element = prelevel .. "[" .. name .. "] = " .. value .. comlevel else element = prelevel .. value .. comlevel end elseif (valtype == "table") if (namtype == "string") element = prelevel .. name .. " =" elseif (numbers == 1) element = prelevel .. "[" .. name .. "] =" else element = "" end elseif (valtype == "nil") if (namtype == "string") element = prelevel .. name .. " = nil" .. comlevel elseif (numbers == 1) element = prelevel .. "[" .. name .. "] = nil" .. comlevel else element = prelevel .. "nil" .. comlevel end else element = prelevel .. "-- unknown object type " .. valtype .. " object " .. name end if (verbose == 1) element = element .. " -- " .. valtype .. ", tag: " .. tag(value) end if (((valtype == "table") , (namtype == "number") , (numbers == 0)) or (collapse == 1)) __tdprint(element, 0) else __tdprint(element, 1) end if (valtype == "table") __tdprint(prelevel .. "{", collapse == 0) __tdsorthash(__tdparse, value, level + 1, verbose, numbers, collapse) __tdprint(prelevel .. "}" .. comlevel, 1) end end function __tdsorthash(func, tabl, level, verbose, numbers, collapse) local typesarray = {} local typescount = {} local keycount = 1 local keyarray = {} i, icount in tabl local thistype = type(icount) if not (typesarray[thistype]) typescount[thistype] = 0 typesarray[thistype] = {} end typescount[thistype] = typescount[thistype] + 1 typesarray[thistype][typescount[thistype]] = end sort(typesarray) i, icount in typesarray sort(icount) j, jcount in icount keyarray[keycount] = tostring(jcount) keycount = keycount + 1 end end i, icount in keyarray local tempcount = tonumber(icount) if (tempcount) icount = tempcount end func(icount, tabl[icount], level, verbose, numbers, collapse) end end function __tdprint(instring, newline) __tdoutputstring = __tdoutputstring .. instring if (newline == 1) __tdoutputstring = __tdoutputstring .. "\n" end end function __tdprintglobals() __tdoutputstring = "" __tdprint("globals =", 1) __tdprint("{", 1) __tdsorthash(__tdparse, globals(), 1, 0, 0, 0) __tdprint("}\n", 1) local writefile = "$test_globals_write.lua" writeto(writefile) write(__tdoutputstring) writeto() end __tdprintglobals()
not tables have property. can overwritten.
why not traverse table using loop? or if possible, use lua 5.3 ;)
in lua called table loop, in modern lua it's called generic loop.
the table statement traverses pairs (index,value) of given table. has following syntax:
stat ::= name `,' name in exp1 block end
a statement like
for index, value in exp block end
is equivalent code:
do local _t = exp local index, value = next(t, nil) while index block index, value = next(t, index) end end
note following:
_t invisible variable. name here explanatory purposes only.
the behavior undefined if assign index inside block.
- the behavior undefined if change table _t during traversal.
the variables index , value local statement; cannot use values after ends.
you can use break exit for. if need value of index or value, assign them other variables before breaking.
the order table elements traversed undefined, numerical indices. if want traverse indices in numerical order, use numerical for.
refer lua manual 4.4.4
Comments
Post a Comment