The Table constructor provides a general structure for associative storage. This type provides hash tables in which data objects can be saved according to keys of any type. For a given table, specific types must be chosen for the keys and entries.
In this example the keys to the table are polynomials with integer coefficients. The entries in the table are strings.
t: Table(Polynomial Integer, String) := table()
Type: Table(Polynomial Integer,String)
To save an entry in the table, the seteltseteltTable operation is used. This can be called directly, giving the table a key and an entry.
setelt(t, x^2 - 1, "Easy to factor")
Alternatively, you can use assignment syntax.
t(x^3 + 1) := "Harder to factor"
t(x) := "The easiest to factor"
Entries are retrieved from the table by calling the elteltTable operation.
This operation is called when a table is applied to a key using this or the following syntax.
Parentheses are used only for grouping. They are needed if the key is an infixed expression.
t.(x^2 - 1)
Note that the elteltTable operation is used only when the key is known to be in the table—otherwise an error is generated.
t (x^3 + 1)
You can get a list of all the keys to a table using the keyskeysTable operation.
Type: List Polynomial Integer
If you wish to test whether a key is in a table, the searchsearchTable operation is used. This operation returns either an entry or “failed”.
The return type is a union so the success of the search can be tested using case.
search(x^2, t) case "failed"
The removeremoveTable operation is used to delete values from a table.
If an entry exists under the key, then it is returned. Otherwise removeremoveTable returns “failed”.
The number of key-entry pairs can be found using the # #Table operation.
Just as keyskeysTable returns a list of keys to the table, a list of all the entries can be obtained using the membersmembersTable operation.
Type: List String
A number of useful operations take functions and map them on to the table to compute the result. Here we count the entries which have Hard as a prefix.
count(s: String +-> prefix?("Hard", s), t)
Other table types are provided to support various needs. \indent
AssociationList gives a list with a table view. This allows new entries to be appended onto the front of the list to cover up old entries. This is useful when table entries need to be stacked or when frequent list traversals are required. See AssociationListXmpPage for more information.
EqTable gives tables in which keys are considered equal only when they are in fact the same instance of a structure. See EqTableXmpPage for more information.
StringTable should be used when the keys are known to be strings. See StringTableXmpPage for more information.
SparseTable provides tables with default entries, so lookup never fails. The GeneralSparseTable constructor can be used to make any table type behave this way. See SparseTableXmpPage for more information.
KeyedAccessFile allows values to be saved in a file, accessed as a table. See KeyedAccessFileXmpPage for more information.