Page 1 of 1

Dynamic variables

Posted: Tue Nov 06, 2012 10:13 pm
by r0b1979
Hi all,

I was wondering if it was somehow possible to generate or declare variables dynamically within a TI process. Say, i would like a loop to generate a new variable, but only if a condition is met. As an example i think of looping through cube-dimensions and assign a variable (SrcDim1 -> SrcDimN) and give them the current Dimension name as value.

Simplified example:

DimCount=1;
While(TABDIM(SrcCube, DimCount) @<>'');
CurDim = TABDIM(SrcCube, DimCount);
SrcDim | Dimcount = CurDim;
DimCount = DimCount + 1;
END;

Obviously this 'concatenation' of SrcDim and DimCount wouldn't work, but i was wondering if there is any way to achieve this. I have searched extensively and did not find any workable solution that actually creates new variables when and only if needed. I only found general code which works for a fixed maximum number of (in this case) dimensions.

So, since I believe this is actually not possible I was about to give up on this idea. But of course i wouldn't do this without consulting all of your valuable experience...Anyone with a solution or workaround? Or confirmation that this IS indeed impossible?

Thx and regards,
Rob

Re: Dynamic variables

Posted: Tue Nov 06, 2012 10:25 pm
by mattgoff
I don't believe it's possible, and I believe this is generally frowned upon even in languages which can do it (arrays or hashes are preferred, not that they're available in TM1 either. Even Fortran had arrays FFS).

Matt

Re: Dynamic variables

Posted: Wed Nov 07, 2012 12:23 am
by tomok
There is a little box on the top right called Search. Type in "dynamic variables" and click on the Search button.

Re: Dynamic variables

Posted: Wed Nov 07, 2012 8:16 am
by r0b1979
@mattgoff
Thanks for your reply and i believe you're right. Was hoping for a solution though.

@tomok
Off course i did that, and obviously i did not find the answer i was looking for or i would have saved myself the trouble of opening a new topic. I apologise if I have missed some previous discussion regarding the same issue.

Re: Dynamic variables

Posted: Wed Nov 07, 2012 8:56 am
by lotsaram
I think Tom might have been referring to this topic on Indirect Variables (as opposed to "dynamic variables"). Which you can find if you know what you're looking for.

I don't think there is a way to dynamically create variables as such. The best you can do is to explicitly create/declare each any every member of an "array" in advance to the maximum number of variables you might need and dynamically grab the variable name and value using the Expand function at runtime.

However what you can do dynamically is create temporary dimensions, elements and data and use these like you would a "dynamic array variable".

Re: Dynamic variables

Posted: Wed Nov 07, 2012 10:06 am
by Steve Rowe
If you really need an array like structure in a TI why not use a cube? It's pretty much an identical structure and can be built on the fly. I posted some code an age ago for creating array cubes.

Cheers,

Re: Dynamic variables

Posted: Wed Nov 07, 2012 10:31 am
by r0b1979
@Lotsaram/Steve,

Thanks for the suggestions, i am going to look into arrays in temporary dimensions/cubes, see if that could help. I don't have a specific case but was just looking for ways to simplify creation of new TI Processes.

Regards,
Rob

Re: Dynamic variables

Posted: Wed Nov 07, 2012 12:03 pm
by Harvey
If I need small arrays, I often use a comma separated list and parse it using SCAN and SUBST. It's pretty easy once you get used to it.

If you want examples, the easiest way is to dig into Bedrock's open source TI processes. Here's an example, adapted from the Dim.Sub.Create.ByElement process:

Code: Select all

pDelimeter = ',';
sElements = 'A,B,C,D,E,F';
nDelimIndex = 1;

# Split filter into seperate dimensions
While( nDelimIndex <> 0 & Long( sElements ) > 0 );
  nDelimIndex = Scan( pDelimiter, sElements );
  If( nDelimIndex <> 0 );
    sElement = Trim( SubSt( sElements, 1, nDelimIndex - 1 ) );
    sElements = Trim( SubSt( sElements, nDelimIndex + Long( pDelimiter ), Long( sElements ) ) );
  Else;
    sElement = Trim( sElements );
  EndIf;
End;
Bedrock is also a very good way to speed up TI development in general. You can get it here.

Re: Dynamic variables

Posted: Thu Nov 08, 2012 9:32 am
by r0b1979
Good suggestions Lazarus, they look helpful. Thank you.