Page 1 of 1

Variable variable names

Posted: Wed May 18, 2011 9:16 am
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

Re: Variable variable names

Posted: Wed May 18, 2011 12:21 pm
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.

Re: Variable variable names

Posted: Wed May 18, 2011 12:25 pm
by lotsaram
Look up the EXPAND function in the reference guide. It may be what you are looking for.

Re: Variable variable names

Posted: Wed May 18, 2011 1:06 pm
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

Re: Variable variable names

Posted: Wed May 18, 2011 1:29 pm
by prameson

Re: Variable variable names

Posted: Wed May 18, 2011 1:47 pm
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

Re: Variable variable names

Posted: Thu May 19, 2011 8:49 am
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

Re: Variable variable names

Posted: Fri May 20, 2011 8:33 am
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

Re: Variable variable names

Posted: Wed Aug 12, 2015 10:47 am
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