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:
- “=” to set a value in a collection.
- “+” or “add” to add a value to a collection.
- “-” or “remove” to remove a value from a collection.
- “+>” or “include” to add multiple values to a collection as individual values.
- “->” or “exclude” to remove multiple values from a collection as individual values.
- “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)
Nested list or map items can be modified by providing the appropriate positions or key values in sequence, separated by a space, following the variable name, and before the assignment of the value.
Example:
$map = a: (one, two, three), b: (four, five, six) $map a 1 = zero print ($map a 1) > zero
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
Contains logical operator
The ‘contains’ logical operator can be used inside comparison logic commands in combination with lists or maps, to see whether a given value, key, or path exists inside of a collection.
The syntax for this looks like this:
[<list>/<map>] (!)contains (<path>) [<list value>/<map key>]
Where the optional path may be list positions or map keys. In the case of nested lists or maps, multiple steps may be specified, separated by a space. The path must always be followed by the literal value of the item in the list, or the key in the map.
Examples:
$list = one, two, three if $list contains one print Success!
$map = a: one, b: two, c: three if $map contains a print Success!
$nested = a: (first: (one, two), second: (three, four)), b: (third: (five, six)) if $nested contains b third five print Success!