Variable variable names

Post Reply
holger_b
Posts: 131
Joined: Tue May 17, 2011 10:04 am
OLAP Product: TM1
Version: Planning Analytics 2.0
Excel Version: 2016
Location: Freiburg, Germany

Variable variable names

Post by holger_b »

Hi all,

is there a way to use variable variable names in a Turbo Integrator process? I want to fill a range of 25 variables from P0 to P24. I hate to rewrite that code for each of those 25 variables; so what I have in mind is something like this:

i=0;
while (i<=24);
varName = 'P' | NumberToString(i);
Result = doSomeWeirdCalculation();
%%varName = Result; (or what ever the proper syntax might be)
i = i+1;
endif;

Any hope?

Thank you
Holger
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: Variable variable names

Post by tomok »

Short answer is no and I don't know why you would need to do that, quite frankly. Variable names are meaningless to the actual execution of code. People come up with all kinds of schemes to name them but that is just for organization's sake. If you WERE able to do that (rename a variable), it would do you no benefit since that would all happen inside the execution of the script and you would never see it.
Tom O'Kelley - Manager Finance Systems
American Tower
http://www.onlinecourtreservations.com/
lotsaram
MVP
Posts: 3703
Joined: Fri Mar 13, 2009 11:14 am
OLAP Product: TableManager1
Version: PA 2.0.x
Excel Version: Office 365
Location: Switzerland

Re: Variable variable names

Post by lotsaram »

Look up the EXPAND function in the reference guide. It may be what you are looking for.
holger_b
Posts: 131
Joined: Tue May 17, 2011 10:04 am
OLAP Product: TM1
Version: Planning Analytics 2.0
Excel Version: 2016
Location: Freiburg, Germany

Re: Variable variable names

Post by holger_b »

tomok,

seems like I failed to make my intention clear. I would prefer to loop through those 25 variables with just some five lines of code instead of re-writing the code for all of them.

Somehow I could not yet figure out how the EXPAND() example (which I had seen before) could help me here, but I keep on trying.

Regards
Holger
prameson
Posts: 16
Joined: Wed May 11, 2011 11:36 am
OLAP Product: TM1
Version: 2.09
Excel Version: Office 365 E5
Location: London

Re: Variable variable names

Post by prameson »

holger_b
Posts: 131
Joined: Tue May 17, 2011 10:04 am
OLAP Product: TM1
Version: Planning Analytics 2.0
Excel Version: 2016
Location: Freiburg, Germany

Re: Variable variable names

Post by holger_b »

Thank you prameson, but all that just solves the reading of variables, but not the dynamic addressing of them in order to write into them.

In TCL (Cubeware Importer) it would work like this:

puts "Enter a variable name:"
gets stdin varname
set $varname 42
puts "I have set variable $varname to [set $varname]"

(a great web site re this btw: http://rosettacode.org/wiki/Dynamic_variable_names)

I guess I have to think of other ways.

Thanks to all of you
Holger
User avatar
Martin Ryan
Site Admin
Posts: 1989
Joined: Sat May 10, 2008 9:08 am
OLAP Product: TM1
Version: 10.1
Excel Version: 2010
Location: Wellington, New Zealand
Contact:

Re: Variable variable names

Post by Martin Ryan »

You could use a dimension as your variable holder. I'm not sure how many lines of code it'll actually save, but something like this (assuming the dimension TempDim already exists and is prepopulated with 25 elements, though you could do this step in the prolog if you wanted).

sDim='TempDim';
i=1;
while(i<25);
sElem='P' | i;
result=docalc;
attrputs(result, sDim, sElem, 'AttrName');
i=i+1;
end;

I suspect that doesn't quite answer your question (as I'm not entirely sure I understand your question), but it may help point you in the right direction.

Martin
Please do not send technical questions via private message or email. Post them in the forum where you'll probably get a faster reply, and everyone can benefit from the answers.
Jodi Ryan Family Lawyer
holger_b
Posts: 131
Joined: Tue May 17, 2011 10:04 am
OLAP Product: TM1
Version: Planning Analytics 2.0
Excel Version: 2016
Location: Freiburg, Germany

Re: Variable variable names

Post by holger_b »

Martin, now this is a really smart way of dealing with it. Actually I found a way to redesign the code so I do not need that any longer, but I will keep your suggestion in mind for similar cases.

Thank you
Holger
holger_b
Posts: 131
Joined: Tue May 17, 2011 10:04 am
OLAP Product: TM1
Version: Planning Analytics 2.0
Excel Version: 2016
Location: Freiburg, Germany

Re: Variable variable names

Post by holger_b »

Now some time has passed and my hair has turned grey, I re-read this and found that today I would probably use a string variable to store all those values, maybe like this:

Code: Select all

sString = 'V01=1#V02=9#V03=4#'
Or maybe just '1#9#4#', then reading this input again where ever I need it. Could even be a global variable so it would be available in other processes. Or, in cases where I do not mind the extra effort, I note things in a csv file and read them again later with a separate process.

Storing the values in an auxiliary dimension sure works nicely as well, but this locks the }Dimensons dimension which then inhibits other processes, especially those which use the same trick.

And once I understood the somewhat confusing description of the expand() function in the reference guide, I came to highly estimate the benefit of it. It allows to concatenate the name of a variable and then read the contents of it - I use this a lot:

Code: Select all

i=1;
sVarName = 'V' | NumberToString(i);
nValue = Expand('%sVarName%');
Unfortunately, as stated before, there is no way to write into V1 that way...

Regards
Holger
Post Reply