Collection Operations

In CoyoteScript, variables can hold map or list based collections. These collections can be dynamically modified and read from. Collections can also contain their own collections, allowing them to be nested inside another.

 

Modifying collections

Collections can be modified through collection operations. These operations include:

  1. “=” to set a value in a collection.
  2. “+” or “add” to add a value to a collection.
  3. “-” or “remove” to remove a value from a collection.
  4. “+>” or “include” to add multiple values to a collection as individual values.
  5. “->” or “exclude” to remove multiple values from a collection as individual values.
  6. “new list” or “new map” can be used to create an empty list or map.

Examples:

Adding and removing values to a list:

$list = dog, cat
$list + bird
$list - cat
print $list
> (dog, bird)

Setting the value at a specific position in a list:

$list = dog, cat, bird
$list 2 = mouse
print $list
> (dog, mouse, bird)

Adding multiple values in one list as individual values to another list:

$list new list
$list + dog
$list2 = mouse, monkey
$list +> $list2
print $list
> (dog, mouse, monkey)

Adding map entries requires specifying both the key and the value of the entry, while removing a map entry only requires specifying the key:

$map = a: dog, b: cat
$map + c: bird
$map - b
print $map
> (a: dog, c: bird)

Setting the value at a given key in a map:

$map = a: dog, b: cat, c: bird
$map b = mouse
print $map
> (a: dog, b: mouse, c: bird

 

Reading values

List values can be accessed by their position by putting the list inside parenthesis followed by the position index.

Example:

$list = a, b, c
print ($list 2)
> b

Map values can be accessed by the key value of an item in the map by putting the map inside parenthesis followed by the value of the key.

Example:

$map = a: dog, b: cat, c: bird
print ($map b)
> cat

Nested list or map items can be read by providing the appropriate positions or key values in sequence, separated by a space, within the parenthesis.

Example:

$map= (a: (dog, cat, bird) b: (car, bike, plane))
print ($map a)
> (dog, cat, bird)
print ($map a 2)
> cat