Skip to main content

table

Availability

Device + Agent

Description

Squirrel’s table object provides a set of tools for the manipulation of tables and the slots they contain. A slot is a pairing of a key and the value accessed using that key. Values and keys can be any Squirrel data type.

Slot keys are typically strings which contain meaningful names but they do not need to be. To use non-string keys, use array-style syntax. The following code creates a table with two slots using Boolean true and false as keys:

local aTable = {};
aTable[true] <- "YES";
aTable[false] <- "NO";

This might be used to signal the result of a Boolean test function:

server.log(aTable[testFunction()]);

If testFunction() returns true, the server logs the relevant aTable value: the string "YES".

Referencing

Tables are accessed by reference, and adding a table reference to another entity, such as an array or another table, or passing it into a function parameter, does not add a copy of the table but a reference to it.

For example, the following code

local arrayOne = [];
local arrayTwo = [];
local tableOne = {};

arrayOne.push(tableOne);
arrayTwo.push(tableOne);

Causes the two arrays arrayOne and arrayTwo to each contain a reference to a single, shared table, tableOne.

Delegation

Table objects, like all other Squirrel data types, have a ‘delegate’ — an auxiliary table object which provides a number of generic methods for manipulating values and, in the case of tables, keys. Delegates provide data type conversion methods to integers and floats, for instance, and item and character manipulation methods to arrays and strings.

Unlike other data types, tables have direct access to their delegates, can be assigned alternative, user-defined delegates, and can be accessed with or without recourse to their delegate, default or custom. This is because the table is Squirrel’s fundamental non-scalar data type: classes, their instance objects are implemented using tables. Object methods and properties are functions and variables held in table slots.

Delegation is a powerful technique. It allows one object to extend the functionality of another. For instance, delegation allows an object to provide the methods of its delegate even if it doesn’t implement them itself. Object and delegate can be seen as child and parent, the child ‘inheriting’ the methods of its parent.

Further Information

For more guidance on using tables, please see the Squirrel Programming Guide.

Member Entities

The table object has the following member methods: