Skip to content

Assoc

Name Mandatory Description Default Type
⬅️ Input Input sequence that defines which element in the target sequence or table needs to be updated and with what value. Should have even number of elements. [Any]
Output ➡️ Modified array or table. Has the same type as the array or table on which Assoc was applied. [Any]
Name No The name of the sequence or table to be updated. `` StringVar(Any)
Key No Table key for the value that is to be updated. Parameter applicable if target is table. none Any
Global No If the variable is or should be available to all the wires in the same mesh. The default value (false) makes the variable local to the wire. false Bool

Updates a sequence or a table based on the input sequence.

Details

The input sequence should contain pairs of elements. The first value in each pair specifies the index or key to update, and the second value in the pair provides the new value for the earlier index/key.

For example, [0 1] will update the value at index 0 in the sequence to 1. Similarly, ["firstkey" 1] will update key "firstkey" in the table with the value of 1. When using Assoc to update the values of a table, if the key specified in the input sequence does not exist, it will add it as a new key.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
; Update a sequence (needs to be mutable!)
[10 20] >= sequence
Log ; prints original target sequence => [10 20]
[0 2 1 3] | Assoc(sequence) ; input sequence updates target sequence [index-0 ele => 2, index-1 ele => 3]
sequence | Log ; prints updated target sequence => [2 3] 
sequence | Assert.Is([2 3] Break: true)

; Update a global-var table (Global = true; table available to all the wires in the same mesh)
{key1: [10 20] key2: [30 40]} | Set(tableG Global: true)
Log ; prints original table => {key1: [10 20] key2: [30 40]}
[0 2 1 3] | Assoc(tableG "key1") ; input sequence updates value of key "key1" in table
tableG | Log ; prints updated table => {key1: [2 3] key2: [30 40]}
tableG | Assert.Is({key1: [2 3] key2: [30 40]} Break: true)

; Update a local-variable table (Global = false, table available to only this wire in the mesh)
{key1: [10 20] key2: [30 40]} >= table ; (needs to be mutable!)
Log ; prints original table => {key1: [10 20] key2: [30 40]}
[0 2 1 3] | Assoc(table "key2") ; input sequence updates value of key "key2" in table
table | Log ; prints updated table => {key1: [10 20] key2: [2 3]}
table | Assert.Is({key1: [10 20] key2: [2 3]} Break: true)

[info] [sample-wire] [10 20]
[info] [sample-wire] [2 3]
[info] [sample-wire] {key1: [10 20] key2: [30 40]}
[info] [sample-wire] {key1: [2 3] key2: [30 40]}
[info] [sample-wire] {key1: [10 20] key2: [30 40]}
[info] [sample-wire] {key1: [10 20] key2: [2 3]}