Page 1 of 1
Nested IF statements in TI
Posted: Wed Jun 01, 2011 6:51 pm
by dfrench77
Can someone please let me know why the following code is not compiling in TI on the MetaData tab?
vDateUsed=IF(LONG(vDate)=10);
SUBST(vDate,1,5);
ELSEIF(LONG(vDate)=9);
SUBST(vDate,1,4);
ELSEIF(LONG(vDate)=8);
SUBST(vDate, 1, 3);
ENDIF;
However the following code is compiling:
vDateUsed=If(LONG(vDate)=10, SUBST(vDate, 1, 5), SUBST(vDate, 1, 4));
Per TM1 documentation:
The TurboIntegrator If statement differs from the Rules IF function in that the TurboIntegrator statement can accept multiple ElseIf statements to evaluate multiple expressions, while the Rules IF function can evaluate only one expression.
Thank you.
Re: Nested IF statements in TI
Posted: Wed Jun 01, 2011 6:57 pm
by Alan Kirk
dfrench77 wrote:Can someone please let me know why the following code is not compiling in TI on the MetaData tab?
vDateUsed=IF(LONG(vDate)=10);
SUBST(vDate,1,5);
ELSEIF(LONG(vDate)=9);
SUBST(vDate,1,4);
ELSEIF(LONG(vDate)=8);
SUBST(vDate, 1, 3);
ENDIF;
However the following code is compiling:
vDateUsed=If(LONG(vDate)=10, SUBST(vDate, 1, 5), SUBST(vDate, 1, 4));
Per TM1 documentation:
The TurboIntegrator If statement differs from the Rules IF function in that the TurboIntegrator statement can accept multiple ElseIf statements to evaluate multiple expressions, while the Rules IF function can evaluate only one expression.
You're misunderstanding the syntax. The "block" If test is a flow control statement. It doesn't return a value, it merely evaluates to True or False. If it's True, the next block of line(s) will be executed. If False, they won't be. The "in line" If() function, as you've found, DOES return a value. They serve different purposes. The Block one would work like this:
Code: Select all
IF(LONG(vDate)=10);
vDateUsed=SUBST(vDate,1,5);
ELSEIF(LONG(vDate)=9);
vDateUsed=SUBST(vDate,1,4);
ELSEIF(LONG(vDate)=8);
vDateUsed=SUBST(vDate, 1, 3);
ENDIF;
The "in line" rules If works in TI (as most Rules functions do) but obviously the advantage of the "block" If syntax is that you can execute multiple lines of code for each block, not just do a single value assignment.
Re: Nested IF statements in TI
Posted: Wed Jun 01, 2011 7:04 pm
by dfrench77
Alan - Thanks for that explanation. That makes sense to me now. Your explanation should be included in the TM1 documentation.
Thanks.
Re: Nested IF statements in TI
Posted: Thu Jun 02, 2011 1:05 am
by jrizk
To simply your script you could try:
vDateUsedLen = LONG(vDate) - 5;
vDateUsed=SUBST(vDate,1,vDateUsedLen);
Re: Nested IF statements in TI
Posted: Tue Jul 08, 2014 6:59 am
by anathaw
Hi , we do have a maximum limit to the no. of nested ifelse we can use in a TI. Are you aware of the maximum limit?
Re: Nested IF statements in TI
Posted: Tue Jul 08, 2014 7:20 am
by pandinus
anathaw wrote:Hi , we do have a maximum limit to the no. of nested ifelse we can use in a TI. Are you aware of the maximum limit?
Yes, as far as I can remember this is about 15 or 16.
Re: Nested IF statements in TI
Posted: Tue Jul 08, 2014 8:34 am
by jacktuckerman
I think it may support up to 20, at least it used to.
Also - depending on what version of TM1 you are using, a TI script will actually save without error if you exceed the nested limit, I think any version prior to 9.4-ish.
Re: Nested IF statements in TI
Posted: Tue Jul 08, 2014 9:33 am
by declanr
This is actually one thing where the documentation is quite clear and correct at the same time.
The limit is 20 and for the last few versions at least you get the following error if you hit 21 nested ifs or more:
"Maximum IF stack depth exceeded..."
The ellipses relates to the part where it tries to include the start of your 20+ nested if statement in the error message... because that's going to fit in so nicely...
Re: Nested IF statements in TI
Posted: Tue Jul 08, 2014 9:47 am
by jacktuckerman
Just to add one last thing to this, I think versions 9.4 - 10 (as declanr states after that you get a meaningful error message), the error message is the classically cryptic "possible missing y after x", roughly translated as TI telling you "there's an error somewhere, go find it chump".
Re: Nested IF statements in TI
Posted: Tue Jul 08, 2014 9:53 am
by declanr
jacktuckerman wrote:Just to add one last thing to this, I think versions 9.4 - 10 (as declanr states after that you get a meaningful error message), the error message is the classically cryptic "possible missing y after x", roughly translated as TI telling you "there's an error somewhere, go find it chump".
9.5.2 definitely gives the "Maximum IF stack depth exceeded" message but I'm not in a position to check any earlier than that, certainly wouldn't surprise me that it wouldn't tell you. I doubt it's one that most people encounter anyway but it has been documented since at least 9.4 that the limit is 20.
Re: Nested IF statements in TI
Posted: Tue Jul 08, 2014 11:35 am
by anathaw
thanks for the information
