home / software

ztact.lua


Download

Download ztact-lua.zip.
View ztact.lua.

Overview

The ztact.lua module contains a collection of generally convenient and reusable functions.

I started writing ztact.lua before I wrote pozix.c and pzp.lua. As a result, there are currently some functions in ztact.lua that need to be moved to pzp.lua or removed altogether. This will happen at some point when I clean up ztact.lua.

However, there are useful functions inside ztact.lua that will survive the impending cleanup. They are:


Example usage


-- public domain 20080407 lua@ztact.com


require 'ztact'


-- tree manipulation ---------------------------------------- tree manipulation


-- adding a node...

-- without ztact.set
a = a or {}
a.b = a.b or {}
a.b.c = a.b.c or {}
a.b.c.d = 1

-- with ztact.set:
a = a or {}
ztact.set (a, 'b', 'c', 'd', 1)


-- determining if a node exists...

-- without ztact.get
if a and a.b and a.b.c and a.b.c.d == 1 then
  -- do something
end

-- with ztact.get
if ztact.get (a, 'b', 'c', 'd') == 1 then
  -- do something
end


-- clearing a node (that may not exist!)

-- without ztact.set
-- Note that the below does not collapse the now empty branch
-- a.b.c, it only -- removes the leaf 'd'.
if a and a.b and a.b.c then
  a.b.c.d = nil
end

-- with ztact.set
-- Note that the below will also collapse the empty branch a.b.c, so
-- that a will again be an empty table.
ztact.set (a, 'b', 'c', 'd', nil)

Reference

get (rootnode, key1, key2, ..., keyN)

Recursively descends into rootnode one key at a time. Returns rootnode[key1][key2]...[keyN] or nil if the value associated with any key in the descent path is nil.


set (rootnode, key1, key2, ..., keyN, value)

If value is not nil then:

Recursively descends into rootnode one key at a time, creating new nodes as needed. Sets rootnode[key1][key2]...[keyN] = value and returns value.

If value is nil then:

Sets rootnode[key1][key2]...[keyN] = nil. If the descent path [key2]...[keyN] does not exists, or if it has no other data leaves (but [key1] does), then the [key2] branch will be collapsed (with rootnode[key1][key2] = nil). Returns the old value of the specified descent path, or nil if the descent path did not exist.