Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Table Library

The table library provides functionality for manipulating tables. All of these functions are accessible as methods of table objects, as if they were objects. Also, these functions are available as global functions through the "table" namespace, so that they can be called on tables which overload these functions. Therefore, these functions can be called as "t.methodname()", or as "table.methodname(t)", where "t" is any table object.

Changes from MiniD 1

The table.set and table.get methods have been removed. Simply replace these with rawSet and rawGet from the baselib.

t.dup()

Duplicates the table. The returned table has the same key-value pairs as the source table. This is only a "shallow" copy; the keys and values themselves are not copied.

t.keys()

Returns an array of all the keys in the table. This is a nontrivial function, so don't call this often in performance-intensive code. The order of the elements is arbitrary.

t.values()

Returns an array of all the values in the table. This is a nontrivial function, so don't call this often in performance-intensive code. The order of the elements is arbitrary.

t.opApply()

This allows you to iterate over the key-value pairs in a table with a foreach statement.

foreach(k, v; {x = 5, y = 10})
	// ...

The order in which the key-value pairs are traversed is arbitrary. Because of the way this function is implemented, it is also legal to remove key-value pairs from the table (by setting a value to null) in the loop. Adding them is also legal, but newly-added pairs will not appear as indices in the loop.

t.each(body: function)

This function provides an alternative way of iterating through a table. You can sacrifice a little bit of flexibility for a bit more performance with this function. Rather than writing a foreach loop, you can pass this function a callback, or the "body" of the loop. The table will be iterated over, calling the callback function for each key-value pair.

The callback function takes two parameters: the key and the value. The 'this' parameter of the callback function is also set to be the table. If the function wishes to leave the loop early (i.e. where you'd usually use a break statement), the function should return false. Unlike using a foreach loop, it's not legal to remove key-value pairs from the table during iteration. Well, nothing will stop you from doing so, but the iteration will get messed up. The same goes for adding key-value pairs to the table.

Here is some example usage:

local t =
{
	x = 5,
	y = 10,
	z = 15,
	foo = "hi",
	bar = "bye"
}

// Print out the key-value pairs
t.each(function(k, v)
	writefln("table[", k, "] = ", v))

// Looks for v in t, and returns the index where the value is found, or null if not found.
local function findValue(t, v)
{
	local index

	t.each(function(k, val)
	{
		if(val is v)
		{
			index = k

			// Return false to break out of the loop early
			return false
		}
	})
	
	return index;
}

writefln(findValue(t, 15))
writefln(findValue(t, 1325908))