ELEMENT
Function Name |
ELEMENT |
---|---|
Description |
The ELEMENT function allows a Logic program to select parts of a string and return the selected parts as a string value. For example, it can be used to return the fourth value in a comma separated list. |
Arguments |
E {INTEGER} E is the element number. This specifies the location of the string to be extracted. 1 is the first string in the input string, 2 is the second string in the input string and so on. Input 1 {STRING} Input 1 specifies the delimiter (the string that acts as the separator in the full string input). For example, if the string input is a comma separated list, Input 1 will be a comma character as the strings in the input are separated by commas. Input 2 {STRING} Input 2 is the string input from which the ELEMENT output will be taken. It consists of one or more values (each of which is also a string) which are separated by a delimiter. Typically, the delimiter is a comma, but it can be any character or characters. D {INTEGER} D is the Direction. This specifies the starting position - if D is 0, the starting position is the left of the Input 2 string, which means that the output will be calculated from the left-hand side of the string; if D is any other number, the output is calculated from the right-hand side of the string. For more information on the data types for the inputs and outputs, see Data Type Hierarchy. |
Returns |
Output {STRING} The output is the string that is at the defined position in the input string (which contains many separated string values). |
Example:
Function Block Diagram - ELEMENT:
In this example, the ELEMENT function is used to return one string from a list of four. For the example, the input string is a constant with four values, but you could just as easily map a string containing values from a database to a string variable database item and use the string variable as the source of the input.
The state of a digital point is used as the E input. An ADD function is used to add 1 to the input as the digital point states begin at 0 whereas the element numbers begin at 1.
A string constant is used for IN1. It defines the delimiter as a comma.
A constant is used for the D input. As it is 0, the string position is calculated from the left of the input string.
As the ‘State’ direct variable is 1, the value for E is 2 (the ADD function adds an extra 1). This means that the second string value in the string input will be returned. As D is 0, the second string is counted from the left-hand side of the input string, and so the output of the ELEMENT function is ‘Slow’.
ST Program - ELEMENT:
To use an ELEMENT function in an ST program, you need to use this syntax:
- Result := ELEMENT(1, ',', 'Stop,Slow,Fast,Start', 0);
Where the ',', 'Stop,Slow,Fast,Start', and 0 values are the input values (in this case, they are the same values as used in the Function Block Diagram example above).