Tutorial: How to increment a numeric Dynamic Chain Variable
AnsweredUpdate: Variable Transform has been expanded to handle math operations such as incrementing a Dynamic Chain Variable value. The two methods below still work, but are no longer necessary.
If you have a numeric Dynamic Chain Variable that you need to increment within your Chain, you may try to add "current value + 1" in a Set Dynamic Chain Variable command and find that this doesn't work as you may have hoped. Unfortunately, this will not increment the value and the result will be the literal value of "current value + 1" (ie. "0+1" where current value is 0). The following are two possible ways to increment the value.
The three documentation links below should be read prior to continuing.
- Manage variables for the workspace and chains
- Set dynamic chain variables
- Transform variables and outputs
Below are two example ways to increment the dynamic variable. This assumes that you have already created and initialized your Dynamic Chain Variable in Chain Settings. The two methods below will use a Dynamic Chain Variable named "num" with an initial value of 0.
Method 1 - Advanced Query
The Tabular Transformation command Advanced Query is very useful and can be used to accomplish this task. Below are the configurations of this node to do so:
- Tables: select a non-empty comma separated (.csv) file for the File value. The contents of this file are irrelevant for this operation as long as the file is not empty. Then provide a Table Name. An example is "tmptbl".
- Query: SELECT CAST('<num>' AS INTEGER)+1 AS increment FROM tmptbl LIMIT 1
The value "<num>" is selected from the left panel "Select a variable" > Chain > Num. The limit is optional but can be useful if the file used is large.
- Check "Preview results" button to capture results in logs and to help with troubleshooting if needed.
Finally use Extract Value (column 1, row 2) to retrieve the incremented value from the Advanced Query output. Then store the extracted value in the Dynamic Chain Variable. Below are the three nodes for this method:
Method 2 - Regular Expressions
It is possible to increment the Dynamic Chain Variable directly within the Set Dynamic Chain Variable command using regular expressions. To begin, open the Set Dynamic Chain Variable command and click on the value of num to transform it (if num is not already set to the Value, you can do so by selecting it in the left panel "Select a variable" > Chain > Num).
- Select Replace transformation.
Pattern: $
Replacement: ~0123456789
Match Type: RegEx
This finds the end of the text and adds " ~0123456789" to the end. It is important to add the leading blank space. The special character ~ is used as a character that is not expected to appear in your number.
- Add a second Replace transformation.
Pattern: (?=\d)(?:([0-8])(?=.*\1(\d)\d*$)|(?=.*(1)))(?:(9+)(?=.*(~))|)(?!\d)
Replacement: $2$3$4$5
Match Type: RegEx
This will increment the value by 1. If the number is 9, 1 is prepended and a trailing ~ is added after the 9 (ie. 9 becomes 19~) which will be handled in the next step.
- Add a third Replace transformation
Pattern: 9(?=9*~)(?=.*(0))|~| ~0123456789$
Replacement: $1
Match Type: RegEx
This last replacement step will look for a 9 with a trailing ~ and replace it with a 0 (ie. 19~ becomes 10~) and then remove the ~ (ie. 10~ becomes 10). Finally, remove the 10 numbers that were added in the first replacement.
Please sign in to leave a comment.
Comments
0 comments