Page 1 of 1

Deleting even elements from dimension with process

Posted: Thu Jan 28, 2021 10:47 am
by vasek1192
Hi, This is my first post and first time working with TM1 Architect so sorry for elemental question.

Problem is as follows: I created Dimension with 100 elements 1-100, now I need to write separate process that will delete all even numbers from the dimension. I have some idea how to do this in the process creating the dimension but with separate process I dont even know where to start. So I would appreciate some pointers. Thanks.

Code: Select all

prefix = 'A_VNO_';
DimName = prefix | 'G_Rada';
LoopCounter = 1;

IF(DimensionExists(DimName)=0);
DimensionCreate(DimName);
ENDIF;

WHILE(LoopCounter <=100);

   ElName = NumberToString(LoopCounter);
   DimensionElementInsert(DimName, '', ElName, 'N');
   LoopCounter = LoopCounter + 1;

END;

Re: Deleting even elements from dimension with process

Posted: Thu Jan 28, 2021 11:33 am
by lotsaram
How would you do this in any other programming language?
Look up the MOD function.
... and don't forget that when deleting items it is much better to decrement through a list than increment!

Re: Deleting even elements from dimension with process

Posted: Thu Jan 28, 2021 12:57 pm
by vasek1192
Thanks, I solved it by this. Is there any more efficient way to do this? Thanks.

Code: Select all

DimName = 'A_VNO_G_Rada';
LoopCounter = 1;

WHILE(LoopCounter <=100);

IF(MOD(DIMIX(DimName, NumberToString(LoopCounter)),2)=0);

DimensionElementDelete(DimName, NumberToString(LoopCounter));

ENDIF;

LoopCounter = LoopCounter + 1;

END;

Re: Deleting even elements from dimension with process

Posted: Thu Jan 28, 2021 1:46 pm
by declanr
vasek1192 wrote: Thu Jan 28, 2021 12:57 pm Thanks, I solved it by this. Is there any more efficient way to do this? Thanks.
The way you have done it is pretty efficient and will work quickly.
However due to your specific requirement, you could have just started at 2 and worked in increments of 2.

Code: Select all

DimName = 'A_VNO_G_Rada';
LoopCounter = 2;

WHILE(LoopCounter <=100);
   DimensionElementDelete(DimName, NumberToString(LoopCounter));
   LoopCounter = LoopCounter + 2;
END;

Re: Deleting even elements from dimension with process

Posted: Thu Jan 28, 2021 1:57 pm
by vasek1192
Thank you, this is much simpler :)

Re: Deleting even elements from dimension with process

Posted: Thu Jan 28, 2021 2:38 pm
by Steve Rowe
Just to be difficult we also need to make sure we understand the definition of "even". This won't always match off to the index or every other element. Both ways the code is written above make underlying assumptions about the structure of the dimension.
It might be right now but will it be right in the future?
Also think about what happens if your TI is run for a second time...

Code: Select all

DimName = 'A_VNO_G_Rada';
LoopCounter = 1;
Max=DimSiz(DimName);

WHILE(LoopCounter <=Max);
  sElName=Dimnm(DimName , LoopCOunter);
   IF(MOD(Numbr(sElName),2)=0);

    DimensionElementDelete(DimName, sElName);

  ENDIF;

 LoopCounter = LoopCounter + 1;

END;
Edit : Missed a bit of the MOD function out