HyperMath

Table Indexing

Table Indexing

Previous topic Next topic No expanding text in this topic  

Table Indexing

Previous topic Next topic JavaScript is required for expanding text JavaScript is required for the print function  

As mentioned above, table indexing is one-based by default. This means tables are created as follows:

 t = { 3,2,5,1,4 }

Tables returned by HyperMath functions are one-based indexed by default (unless noted otherwise). Tables are versatile enough to accommodate non-positive integers as indices. For example, the following are valid:

> t = {}

> t[0] = 3

> t[-1] = 2

The above can be useful in some situations, like using the data as indices to match corresponding values to avoid lookup tables. However, using non-positive integer indices should be avoided when possible.

In fact, indices for tables are more than just numeric entities; they are like keys in a database. Strings can also be used for indexing:

> t = {}

> t[0] = 3

> t["ID"] = 2

Hence, table indexing has no bounds checking, and invalid indexing results in a nil return.

Indices and corresponding entries can be accessed by the pairs() function as in:

> for i,v in pairs(t) do print(i,v) end

ID    2

0    3

There are two utility functions, GetTableIndices and GetTableKeys, that allow querying table indices by type.  See the topic Utility Functions > Operations.