Skip to main content

Set

The Set SFCodeBlock is used to assign values to variables.

The following table provides information regarding the Set SFCodeBlock.

Set SFCodeBlock

CategoryVariable
NameIf not yet defined, 'Set' allows you to define a new variable assigning the expression result to it. Variables can be referred by any other SFCodeBlocks. A Set code block on an existing variable changes its value and eventually its tye, according to the value returned by the expression. The Set SFCodeBlock could be used to create and modify any type of variable including complex data types (Table, TableRow, List and Dictionary).
InputAF Case Element
ParametersNone
Pattern SupportYes
ReturnsNone
ConfigurationYou are required to define a variable name and enter at least an expression.

To Add a Set Name and Expression
1. Click the Add button.

Select - Add a Set


2. Type the name and the expression in the configuration panel that opens. (The variable name is automatically recognized, if you enter the expression.)
3. Click Save

To define a name and enter an expression:

To Edit a Set Name or Expression:

1. Click on the expression placeholder to open it's configuration panel.
Select - Select - Edit an Expression


2. Type your changes in the configuration panel.3. Click Save.

Set Configuration Panel

The Set configuration panel allows you to enter a variable expression for the set statement. The following table describes the configuration options.

ItemDescription
NameExpression returning the value to be assigned to the variable. Only valid Variable names are accepted, variable name shall start with:

$ for Element variable, the variable is associated to the element
$$ for Analytic variable, the variable is global
_ for element discard variable, no store
__ for analytic discard variable, no store

Example: $var=<Expression>

Please refers to the Expression manual for more details on how to define variables and expressions.
Important Note

Assigning an analytic variable or an analytic discard variable results in a single evaluation of the expression. Notice that when using a discard variable, the result is not stored.
Assigning an element variable or an element discard variable, the expression is evaluated per each element belonging to the Working Space. Notice that when using a discard variable, the result is not stored.

Set - Variables

Variable TypeExpression Is EvaluatedNote
AnalyticOnceAs the variable is global, it does not make any sense assigning it multiple time. Notice that when assigning Analytic variables, you cannot reference elements.
ElementPer each element
Analytic discardOnceWorks like an analytic variable, but the expression result is not stored. As an example, use this syntax for defining a table in an analytic variable.
Element DiscardPer each elementWorks like an element variable, but the expression result is not stored. Use this syntax when executing functions per each element. As an example, you can insert a record in a table per each element.
tip

Use a discard variable with the expression, when you intend to execute a function such as table insert Interlocked, etc. and you don't care about the result of the function.

  • Use the Element Discard variable, if the expression has to be evaluated per each element.
  • Use Analytic Discard variable, if the expression has to be evaluated only once.

Examples: Using 'Set' code block to define simple data types

Set - Simple Data Types

ExpressionDescription
$V = 0Create and initialize a numeric variable assigning the value zero.
$V = ABS("o_Rec_Mass_Test1")Create and initialize a numeric variable assigning the absolute value of the 'o_Rec_Mass_Test1' attribute.
$S = 'This is a string'Create and initialize a string variable assigning a string value.
$$G = 1Create and initialize a numeric analytic variable assigning the value 1.
$B = TrueCreate and initialize a Boolean variable assigning the value True.

Examples: Using 'Set' code block to define complex data types

Variables in SFHub Studio can store complex data types such as List, Table, Table Row, Dictionary, or a reference to an element. These types of variables can be defined and initialized using the built-in functions of SFHub. Please refer to:

The following table provides some examples on how to use the 'Set' code block to define and initialize variables of complex data types.

Set - Expressions to Define Complex Data Types

ExpressionDescription
$$myList = CollectionCreate(false ; 0)Create and initialize an Analytic variable as empty List of numbers (Refer to the SFHub List functions.)
$myList = Split('aaa,bbb,ccc' ; ',')Create and initialize an Element variable (one per each element) as a List of string values. (Refer to the SFHub Strings functions.)
$$myTable = DefineTable ('myTable'; 'Column1'; ''; 'Column2'; 0)Create an Analytic variable as a table with two columns. (Refer to the SFHub Table functions.)
$R = TableNewRow ($$myTable)Create and initialize an Element variable $R as a row for the table referenced by the variable $$myTable. (Refer to SFHub TableRow functions.)
$$myDictionary['FirstKey'] = 0Create and initialize an Analytic variable as a Dictionary assigning a value pair: Key = 'FirstKey' Value = 0
$ $myDictionary[Material] = DensityCreate and initialize an Analytic variable as a Dictionary assigning a value pair: Key = Value of the attribute whose name contains Material. Value = Value of the attribute whose name contains Density.

Examples: Using 'Set' code block to manipulate complex data types*

The following table provides some examples of how to use the 'Set' code block to manipulate variables of complex data types.

Set - Expressions to Manipulate Complex Data Types

ExpressionDescription
__ = ListAdd ($$myList; 4; 5; 6)Append three numeric values to the list referenced by the variable \$\$myList. (Refer to the SFHub List functions.).
Notice that an analytic discard variable is used, as we intend to add new values inside the analytic variable $$myList only once.
_ = TableInsert ($$myTable; 'Element; {Name} ; 'Column2'; 1)Append a new row, with the element name, to the table referenced by the variable $$myTable. (Refer to the SFHub Table functions.).
Notice that an element discard variable is used, as we intend to add a row per each element belonging to the Working Space, but we don't care about the 'TableInsert' return status.
_ = TableAddRow (\$\$myTable; \$R)Append a new row stored in the variable \$R to the table referenced by the variable \$\$myTable. (Refer to SFHub TableRow functions.)
__ = \$\$myDictionary['SecondKey'] = 1Add a new item assigning the value 1 to the key = 'SecondKey'
__ = $$myDictionary['FirstKey'] = 10Modify an existing item with Key ='Firstkey'