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 (array) or a table (associative array/ dictionary) on the basis of an input sequence.

Details

This shard can be used to update specific member elements within a sequence or a table with new values.

The input sequence identifies which elements are to be updated and their new/ updated values. To achieve this, the member elements of this input sequence are parsed in pairs. The 1st element of each pair gives the index of the target element to update, and the 2nd element of that pair gives the new value for the target element. Due to this, the input sequence must always contain an even number of elements.

Examples

1
2
3
4
5
6
;; Update a global-var table (Global = true; table available to all the wires in the same mesh)
{key1: [10 20] key2: [30 40]} | Set(Name: tableG Global: true)
Log("Original Table") ;; 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("Modified Table") ;; prints updated table => {:key1 [2 3] :key2 [30 40]}
tableG | Assert.Is({key1: [2 3] key2: [30 40]} true)


 

1
2
3
4
5
6
;; 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(Name: table Key: "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]} true)


 

1
2
3
4
5
6
;; Update a sequence (needs to be mutable!)
[10 20] >= sequence
Log("Original Sequence") ;; 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("Modified Sequence") ;; prints updated target sequence => [2 3] 
sequence | Assert.Is([2 3] true)