IF statement on single line

Post Reply
User avatar
gtonkin
MVP
Posts: 1265
Joined: Thu May 06, 2010 3:03 pm
OLAP Product: TM1
Version: Latest and greatest
Excel Version: Office 365 64-bit
Location: JHB, South Africa
Contact:

IF statement on single line

Post by gtonkin »

@admins - please move to Tips and Tricks as I cannot post there directly.

This one is mildly interesting but thought some folks may find it saving them a few keystrokes here and there.
Instead of writing the following in TI:

Code: Select all

IF(SUBST(vDim, 1, 1)@='}');
ITEMSKIP;
ENDIF;
You can simply write:

Code: Select all

IF(SUBST(vDim, 1, 1)@='}', ITEMSKIP, 0);
The "0" seems to be continue i.e. do nothing for the ELSE/FALSE part of the IF statement.
Not finding any documentation on this in the Dev or TI guide - IF is probably not the best keyword to use either.
Any comments on this or other little/unknown tips to save fingertips?
BR, George.

Learn something new: MDX Views
Alan Kirk
Site Admin
Posts: 6667
Joined: Sun May 11, 2008 2:30 am
OLAP Product: TM1
Version: PA2.0.9.18 Classic NO PAW!
Excel Version: 2013 and Office 365
Location: Sydney, Australia
Contact:

Re: IF statement on single line

Post by Alan Kirk »

gtonkin wrote:@admins - please move to Tips and Tricks as I cannot post there directly.

This one is mildly interesting but thought some folks may find it saving them a few keystrokes here and there.
Instead of writing the following in TI:

Code: Select all

IF(SUBST(vDim, 1, 1)@='}');
ITEMSKIP;
ENDIF;
You can simply write:

Code: Select all

IF(SUBST(vDim, 1, 1)@='}', ITEMSKIP, 0);
The "0" seems to be continue i.e. do nothing for the ELSE/FALSE part of the IF statement.
Not finding any documentation on this in the Dev or TI guide - IF is probably not the best keyword to use either.
Any comments on this or other little/unknown tips to save fingertips?
Essentially the difference is that the block version is the TI implementation of If, and the single line is the Rules implementation of If which, being a Rules function, is of course available to TI as well. You're correct in your assessment of what it does and how.

It's not one of the more esoteric pieces of coding knowledge, but it's probably true that a lot of people "know" it (in the sense that they know you can use most Rules functions in TI) but don't know that they know it (in the sense that they've never really bothered to try to apply the Rules If() function when TI has its own perfectly serviceable syntax.)

I saw the Rules version used in some early code (early 2000's) that I inherited (and indeed it was often used for ItemSkip), and I probably used it myself a few times back then, but I moved away from it toward the block command. Not because I think there's anything actually wrong with using the Rules If() function, simply because I find the block If makes the code more obvious and self-documenting. Also there's the fact that the Rules If essentially only works if you need to execute a single command like Itemskip; for any multi-line blocks you would have to use the Rules If block anyway which, IMHO would make the coding style more inconsistent and harder for anyone who comes after you to read.

If the True condition is a single line I think this one is down to a question of personal taste. My taste runs to not using it, but that's not to say that I think it's "wrong" to do so.
"To them, equipment failure is terrifying. To me, it’s 'Tuesday.' "
-----------
Before posting, please check the documentation, the FAQ, the Search function and FOR THE LOVE OF GLUB the Request Guidelines.
pandinus
Posts: 78
Joined: Tue Mar 18, 2014 8:02 am
OLAP Product: TM1, Cognos Express
Version: 10.2.2
Excel Version: 2013

Re: IF statement on single line

Post by pandinus »

I frequently use this, as in the following:

Code: Select all

vVariable = IF(condition, vTrueVariable, vFalseVariable);
This makes for easy variable setting, requiring less lines of code.
Alan Kirk
Site Admin
Posts: 6667
Joined: Sun May 11, 2008 2:30 am
OLAP Product: TM1
Version: PA2.0.9.18 Classic NO PAW!
Excel Version: 2013 and Office 365
Location: Sydney, Australia
Contact:

Re: IF statement on single line

Post by Alan Kirk »

pandinus wrote:I frequently use this, as in the following:

Code: Select all

vVariable = IF(condition, vTrueVariable, vFalseVariable);
This makes for easy variable setting, requiring less lines of code.
That's a slightly different situation though. It's evaluating a value rather than executing flow control. Evaluating a value is arguably a Rules "thing" so it's not unreasonable to use the Rules function for that.

It's just mixing and matching flow control syntax that I'm not a fan of. But hey, Iboglix helps us with that by providing so few of them. For loops? Select case? Aaah, who needs 'em?
"To them, equipment failure is terrifying. To me, it’s 'Tuesday.' "
-----------
Before posting, please check the documentation, the FAQ, the Search function and FOR THE LOVE OF GLUB the Request Guidelines.
Wim Gielis
MVP
Posts: 3240
Joined: Mon Dec 29, 2008 6:26 pm
OLAP Product: TM1, Jedox
Version: PAL 2.1.5
Excel Version: Microsoft 365
Location: Brussels, Belgium
Contact:

Re: IF statement on single line

Post by Wim Gielis »

Best regards,

Wim Gielis

IBM Champion 2024-2025
Excel Most Valuable Professional, 2011-2014
https://www.wimgielis.com ==> 121 TM1 articles and a lot of custom code
Newest blog article: Deleting elements quickly
User avatar
Steve Rowe
Site Admin
Posts: 2464
Joined: Wed May 14, 2008 4:25 pm
OLAP Product: TM1
Version: TM1 v6,v7,v8,v9,v10,v11+PAW
Excel Version: Nearly all of them

Re: IF statement on single line

Post by Steve Rowe »

Pretty sure that back in the day we only had the rules version of the IF statement, the block If was a later addition.
Technical Director
www.infocat.co.uk
tomok
MVP
Posts: 2836
Joined: Tue Feb 16, 2010 2:39 pm
OLAP Product: TM1, Palo
Version: Beginning of time thru 10.2
Excel Version: 2003-2007-2010-2013
Location: Atlanta, GA
Contact:

Re: IF statement on single line

Post by tomok »

Steve Rowe wrote:Pretty sure that back in the day we only had the rules version of the IF statement, the block If was a later addition.
Maybe because "back in the day", TurboIntegrator didn't even exist. You had to use processing worksheets. :)
Tom O'Kelley - Manager Finance Systems
American Tower
http://www.onlinecourtreservations.com/
Alan Kirk
Site Admin
Posts: 6667
Joined: Sun May 11, 2008 2:30 am
OLAP Product: TM1
Version: PA2.0.9.18 Classic NO PAW!
Excel Version: 2013 and Office 365
Location: Sydney, Australia
Contact:

Re: IF statement on single line

Post by Alan Kirk »

Steve Rowe wrote:Pretty sure that back in the day we only had the rules version of the IF statement, the block If was a later addition.
It would have to have been in a very, very early model. I remember as far back as Tomok with map sheets in version 6. TI came in with version 7, though I can't recall whether it was there from 7.0. The earliest TI manual I have was for 7.1.3 (before then I think TM1 manuals were all in paper ( :shock: !!!) format) and we see even back then...
TI_07_01_03_Extract.jpg
TI_07_01_03_Extract.jpg (178.98 KiB) Viewed 8634 times
"To them, equipment failure is terrifying. To me, it’s 'Tuesday.' "
-----------
Before posting, please check the documentation, the FAQ, the Search function and FOR THE LOVE OF GLUB the Request Guidelines.
User avatar
Steve Rowe
Site Admin
Posts: 2464
Joined: Wed May 14, 2008 4:25 pm
OLAP Product: TM1
Version: TM1 v6,v7,v8,v9,v10,v11+PAW
Excel Version: Nearly all of them

Re: IF statement on single line

Post by Steve Rowe »

Pretty sure I can remember the very first version of TI not having the block IF, as I can remember life becoming much easier process control wise when it came out.

Maybe they just forgot to document it...how times change!

Cheers
Technical Director
www.infocat.co.uk
Post Reply