Dictionary

A dictionary is a container that maps one value, called the key, to another another value.

property Dictionary<String, String> strings
property Dictionary<Item, Rectangle> rectangles
property Dictionary<Int, CustomButton> buttons

You can create a dictionary directly using the dictionary literal syntax shown below.

var animals = [ "Dog" : "Mammalia", "Goldfish" : "Osteichthyes" ]

As with Arrays, you use the subscript syntax for accessing a key's value. If the key does not exist in the dictionary, a default value is returned.

System.log("A Dog belongs to class " + animals["Dog"])

New keys can also be added, and existing keys modified, by direct assignment using the same subscript syntax.

animals["Earthworm"] = "Oligochaeta"

property Int lengthread only

The number of entries in the dictionary.

var animals = [ "Dog" : "Mammalia", "Goldfish" : "Osteichthyes" ]
System.log(animals.length) // prints '2'

function clear()

Clear the dictionary.

function keys() → Array

Return an array containing the keys in the dictionary. The order of keys is undefined.

The keys array can be used to loop over all entries in a dictionary.

var legCounts = [ "dog": 4, "emu": 2, "centipede": 100 ]

for var animal in legCounts.keys() {
    System.log("A {0} has {1} legs".arg(animal, legCounts[animal]))
}
// A emu has 2 legs
// A dog has 4 legs
// A centipede has 100 legs

function values() → Array

Return an array containing the values in the dictionary. The order of values is undefined.

function contains(keytype key) → Bool

Returns true if the dictionary contains the key, otherwise false.

var animals = [ "Dog" : "Mammalia", "Goldfish" : "Osteichthyes" ]
System.log(animals.contains("Dog")) // prints 'true'
System.log(animals.contains("Cat")) // prints 'false'

function remove(keytype key)

Removes key from the dictionary if it exists and returns true, otherwise does nothing and returns false.

var animals = [ "Dog" : "Mammalia", "Goldfish" : "Osteichthyes" ]
animals.remove("Dog")
System.log(animals.contains("Dog")) // prints 'false'