The table extension is pre-installed in NetLogo.
To use the table extension in your model, add a line to the top of your Code tab:
extensions [table]
If your model already uses other extensions, then it already has an extensions
line in it, so just add table
to the list.
For more information on using NetLogo extensions, see the Extensions Guide
In general, anything you can do with an table in NetLogo, you could also just use a list for. But you may want to consider using an table instead for speed reasons. Lists and tables have different performance characteristics, so you may be able to make your model run faster by selecting the appropriate data structure.
Tables are useful when you need to do associate values with other values. For example, you might make a table of words and their definitions. Then you can look up the definition of any word. Here, the words are the "keys". You can easily retrieve the value for any key in the table, but not vice versa.
Unlike NetLogo’s lists and strings, tables are “mutable”. That means that you can actually modify them directly, rather than constructing an altered copy as with lists. If the table is used in more than one place in your code, any changes you make will show up everywhere. It’s tricky to write code involving mutable structures and it’s easy to make subtle errors or get surprising results, so we suggest sticking with lists and strings unless you’re certain you want and need mutability.
let dict table:make
table:put dict "turtle" "cute"
table:put dict "bunny" "cutest"
print dict
=> {{table: "turtle" -> "cute", "bunny" -> "cutest" }}
print table:length dict
=> 2
print table:get dict "turtle"
=> "cute"
print table:get dict "leopard"
=> (error)
print table:keys dict
=> ["turtle" "bunny"]
table:clear
table:counts
table:from-list
table:get
table:has-key?
table:keys
table:length
table:make
table:put
table:remove
table:to-list
Counts the occurrences of each element of the given list and reports the counts in a table.
Reports a new table with the contents of list. list must be a list of two element lists, or pairs. The first element in the pair is the key and the second element is the value.
Reports the value that key is mapped to in the table. Causes an error if there is no entry for the key.
Reports a list of all the keys in table, in the same order the keys were inserted.
Maps key to value in table. If an entry already exists in the table for the given key, it is replaced.
Reports a list with the content of table. The list will be a list of two element lists, or pairs. The first element in the pair is the key and the second element is the value. The keys appear in the same order they were inserted.