Section9.1

]>
Book Contents Previous Section 8.14 Non-Associative Algebras and Modelling Genetic Laws Next Section 9.2 BalancedBinaryTree Book Index

## 9.1 AssociationList

The AssociationList constructor provides a general structure for associative storage. This type provides association lists in which data objects can be saved according to keys of any type. For a given association list, specific types must be chosen for the keys and entries. You can think of the representation of an association list as a list of records with key and entry fields.

Association lists are a form of table and so most of the operations available for Table are also available for AssociationList. They can also be viewed as lists and can be manipulated accordingly.

This is a Record type with age and gender fields.

Data := Record(monthsOld : Integer, gender : String)

Record(monthsOld:Integer,gender:String)

Type: Domain

In this expression, al is declared to be an association list whose keys are strings and whose entries are the above records.

al : AssociationList(String,Data)
Type: Void

The tabletableAssociationList operation is used to create an empty association list.

al := table()

table()

Type: AssociationList(String,Record(monthsOld: Integer,gender: String))

You can use assignment syntax to add things to the association list.

al."bob" := [407,"male"]$Data

[monthsOld=407, gender="male"]

Type: Record(monthsOld: Integer,gender: String)
al."judith" := [366,"female"]$Data

[monthsOld=366, gender="female"]

Type: Record(monthsOld: Integer,gender: String)
al."katie" := [24,"female"]$Data

[monthsOld=24, gender="female"]

Type: Record(monthsOld: Integer,gender: String)

Perhaps we should have included a species field.

al."smokie" := [200,"female"]$Data

[monthsOld=200, gender="female"]

Type: Record(monthsOld: Integer,gender: String)

Now look at what is in the association list. Note that the last-added (key, entry) pair is at the beginning of the list.

al

table("smokie"=[monthsOld=200, gender="female"],"katie"=[monthsOld=24, gender="female"],"judith"=[monthsOld=366, gender="female"],"bob"=[monthsOld=407, gender="male"])

Type: AssociationList(String,Record(monthsOld: Integer,gender: String))

You can reset the entry for an existing key.

al."katie" := [23,"female"]$Data

[monthsOld=23, gender="female"]

Type: Record(monthsOld: Integer,gender: String)

Use deletedeleteAssociationList to destructively remove an element of the association list. Use deletedeleteAssociationList to return a copy of the association list with the element deleted. The second argument is the index of the element to delete.

delete!(al,1)

table("katie"=[monthsOld=23, gender="female"],"judith"=[monthsOld=366, gender="female"],"bob"=[monthsOld=407, gender="male"])

Type: AssociationList(String,Record(monthsOld: Integer,gender: String))

For more information about tables, see TableXmpPage . For more information about lists, see ListXmpPage .

Book Contents Previous Section 8.14 Non-Associative Algebras and Modelling Genetic Laws Next Section 9.2 BalancedBinaryTree Book Index