Table metatable

Under construction

    • Methods to extend the default functionality of the Lua table type

See also: Custom types are tables / metatables

List metatable

Metatables can be used to extend the default functionality of tables. Create a meta-/extention table once, add it to as many tables as you want and use the functionality of the metatable for all of them, without having to write one line of extra code.

The example below shows part of the table.celx script, that contains the tableMt metatable with a number of table extention functions.

--[[ The metatable
------------------------------------------------------------------------]]
tableMt = {}
tableMt.__index = tableMt

--[[ table function: print()

celestia version

--------------------------------------------------------------------------

returns a shallow print string of the table

remark:

shallow means that if the table contains a second table,

that table's ID will be returned nót the table's content.

input:

title: string, optional

the title to print above the list

------------------------------------------------------------------------]]

function tableMt.print(self, title)

text = title

if text == nil then text = tostring(self) end

for key, value in pairs(self) do

text = text..string.format("\n%-15s = %s", tostring(key), tostring(value))

end

return text

end

--[[ table function: printd()

celestia version

--------------------------------------------------------------------------

returns a deep print string of the table

remark:

deep means that if the table contains a second table,

than that table's content will be printed nót it's ID.

input:

title: string, optional

the title to print above the list

indent: string, optional

the name of the nested table

-> script does not realy indent;

prints indent's value in front of element values

------------------------------------------------------------------------]]

function tableMt.printd(self, title, indent)

text = title

if text == nil then text = tostring(self) end

for key, value in pairs(self) do

if indent == nil then

text = text..string.format("\n%-15s = %s", tostring(key), tostring(value))

else

text = text..string.format("\n%-15s %-15s = %s", indent, tostring(key), tostring(value))

end

if type(value)== "table" then

tableMt.printd(value, text, tostring(value))

end

end

return text

end

Currently tableMt contains the functions:

    • contains(value)
    • indexOf(value)
    • containsValue(value)
    • keyOf(value)
    • print() (shallow, deep)
    • copy() (shallow)

Still to come are a.o. two functions that can do a shallow or a deep print of a table to the Celestia log file.

--[[ Load libraries
========================================================================
    import supporting scripts, etc.
========================================================================
dofile("scripts/celx_libs/table.celx")                       -- the "import"

--[[ Function: Main()

------------------------------------------------------------------------

example of how to use

------------------------------------------------------------------------]]

function Main()

orbitFlags = celestia:getorbitflags() -- returns a table

setmetatable(orbitFlags, tableMt) -- attach tableMt as metatable

celestia:print(orbitFlags:print("Orbit flags:"), 5) -- print the table

end

Downloads

Installation and use

Installation

    1. Download table.celx" to your Celestia\scripts folder or your library folder.

Use

    1. For the discription of how to use it, see the explanation above.

History

Version 0.1, 9 mrt 2011

Initial version (beta)