% Copyright The Numerical Algorithms Group Limited 1992-94. All rights reserved. % !! DO NOT MODIFY THIS FILE BY HAND !! Created by ht.awk. \newcommand{\AssociationListXmpTitle}{AssociationList} \newcommand{\AssociationListXmpNumber}{9.1} % % ===================================================================== \begin{page}{AssociationListXmpPage}{9.1 AssociationList} % ===================================================================== \beginscroll % The \spadtype{AssociationList} constructor provides a general structure for associative storage. %-% \HDindex{list!association}{AssociationListXmpPage}{9.1}{AssociationList} This type provides association lists in which data objects can be saved %-% \HDindex{association list}{AssociationListXmpPage}{9.1}{AssociationList} 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 %-% \HDindex{table}{AssociationListXmpPage}{9.1}{AssociationList} for \spadtype{Table} are also available for \spadtype{AssociationList}. They can also be viewed as lists and can be manipulated accordingly. \xtc{ This is a \pspadtype{Record} type with age and gender fields. }{ \spadpaste{Data := Record(monthsOld : Integer, gender : String) \bound{Data}} } \xtc{ In this expression, \spad{al} is declared to be an association list whose keys are strings and whose entries are the above records. }{ \spadpaste{al : AssociationList(String,Data) \free{Data}\bound{al}} } \xtc{ The \spadfunFrom{table}{AssociationList} operation is used to create an empty association list. }{ \spadpaste{al := table() \free{al}\bound{al1}} } \xtc{ You can use assignment syntax to add things to the association list. }{ \spadpaste{al."bob" := [407,"male"]\$Data \free{al1}\bound{al2}} } \xtc{ }{ \spadpaste{al."judith" := [366,"female"]\$Data \free{al2}\bound{al3}} } \xtc{ }{ \spadpaste{al."katie" := [24,"female"]\$Data \free{al3}\bound{al4}} } \xtc{ Perhaps we should have included a species field. }{ \spadpaste{al."smokie" := [200,"female"]\$Data \free{al4}\bound{al5}} } \xtc{ Now look at what is in the association list. Note that the last-added (key, entry) pair is at the beginning of the list. }{ \spadpaste{al \free{al5}} } \xtc{ You can reset the entry for an existing key. }{ \spadpaste{al."katie" := [23,"female"]\$Data \free{al5}\bound{al6}} } \xtc{ Use \spadfunFromX{delete}{AssociationList} to destructively remove an element of the association list. Use \spadfunFrom{delete}{AssociationList} to return a copy of the association list with the element deleted. The second argument is the index of the element to delete. }{ \spadpaste{delete!(al,1) \free{al6}\bound{al7}} } For more information about tables, see \downlink{`Table'}{TableXmpPage}\ignore{Table}. For more information about lists, see \downlink{`List'}{ListXmpPage}\ignore{List}. \showBlurb{AssociationList} \endscroll \autobuttons \end{page} %