TM1 Break function
- Jun. 20, 2014
Introduction
This article deals with the (undocumented?) Break function in Turbo Integrator (TI). As I did not come across this function earlier, I guess it is worth devoting a blog article on my TM1 website.
We often write While … End loops in Turbo Integrator. In fact, that is the only loop structure that TI allows us to use. No For … Next or Do … While loops that we use in VBA, only While … End (but you can frame the loop into such a construct, so there is not that much need of having other code constructs). For instance, see here for a practical illustration of a loop in TI.
Break function
The Break function can be used to break out of the loop, whenever wanted/desired. It could be necessary to break out of a loop, or it can be handy to break out of a loop (to not process additional useless iterations in the loop, for instance).
Practical illustration
Let's search for the first N-type element within a dimension, and stop the code when found:
##### # Wim Gielis # https://www.wimgielis.com #####vAccount = ''; i = 1; While( i <= Dimsiz( 'PnL_Account' ) ); vAccount = Dimnm( 'PnL_Account', i ); If( Dtype( 'PnL_Account', vAccount ) @= 'N' ); Break; EndIf; End; If( Long( vAccount ) > 0 ); AsciiOutput('First level 0 account.txt', vAccount, NumberToString( i ) ); EndIf;
The text file in the TM1 Database directory contains the found element. Or, in case no such element was found in the loop, the text file will not be created. Without the Break function, you would process the other elements in the dimension as well. The Break function effectively can skip ( Dimsiz( 'PnL_Account' ) - i ) cases.
ProcessBreak
Do not mix up Break and ProcessBreak in Turbo Integrator. ProcessBreak will jump immediately to the Advanced > Epilog tab of the process. This means that you probably break out of too much code :-)
Nested loops
What would happen if you have 2 (or more) nested loops and you use the Break function? Do you break out of all nested loops, or only out of the "current" loop?
##### # Wim Gielis # https://www.wimgielis.com #####n = 1; While( n <= 10); AsciiOutput('File_n.txt', NumberToString( n ) ); nn = 1; While( nn <= 10); AsciiOutput('File_nn.txt', NumberToString( nn ) ); If( nn >= 5 ); Break; EndIf; nn = nn + 1; End; n = n + 1; End;
So, the answer is that you only break out of the current loop and not out of all loops.
Advantages
The Break function is simple and clean. But as stated above, its main advantage is that you do not process otherwise useless iterations of the loop. Not only useless iterations, sometimes you need to prevent the loop from continuing, to pursue a certain goal.
The end
Enjoy your weekend!