Page 1 of 1

Subset Element Exists

Posted: Fri Sep 19, 2008 8:52 am
by MattyG
Hi all,

Is there such a TI function as SUBSETELEMENTEXISTS or similar. I've checked the help files and can't find anything.

Is there a way round this in TI, without using DIMIX?

Cheers,
Matt

Re: Subset Element Exists

Posted: Fri Sep 19, 2008 9:20 am
by Martin Ryan
You could use a while loop with the functions SubsetGetSize and SubsetGetElementName.

Code: Select all

top=SubsetGetSize('Dim', 'Sub');
count=0;
SubElemExists=0;
while(count<top);
if(SubsetGetElementName('Dim', 'Sub', count)@=soughtElement);
SubElemExists=1;
endif;
count=count+1;
end;
Martin

Re: Subset Element Exists

Posted: Fri Sep 19, 2008 9:26 am
by bihints.com
use MDX

SubsetCreateByMDX('Subset 2','{EXCEPT( {[dim].[Subset 1]}, {[dim].[element1]} )}');

then compare the sizes of 'Subset 1' and 'Subset 2' with SubsetGetSize
if both subsets are the same size then it means element1 is not in 'Subset 1'.

Re: Subset Element Exists

Posted: Fri Sep 19, 2008 10:58 am
by MattyG
Thanks Martin,

Whereabouts in the code should I put the SUBSETELEMENTINSERT function?

Cheers,
Matt

Re: Subset Element Exists

Posted: Fri Sep 19, 2008 11:59 am
by Martin Ryan
At the end, like so...

Code: Select all

top=SubsetGetSize('Dim', 'Sub');
count=0;
SubElemExists=0;
while(count<top);
if(SubsetGetElementName('Dim', 'Sub', count)@=soughtElement);
SubElemExists=1;
endif;
count=count+1;
end;
# If element wasn't found in the subset already, then add it in.
if(SubElemExists)=0;
SubsetElementInsert('Dim', 'sub', soughtElement, 1);
endif;

Re: Subset Element Exists

Posted: Fri Sep 19, 2008 1:26 pm
by MattyG
Nearly there . . . .

Count = 1, instead of Count = 0, but the last element in my list is entered into the subset twice, any ideas why?

TIA
Matt

Re: Subset Element Exists

Posted: Fri Sep 19, 2008 4:53 pm
by MattyG
Moving up count = count + 1 :

top=SubsetGetSize('Dim', 'Sub');
count=1;
SubElemExists=0;
while(count<top);
count=count+1;
if(SubsetGetElementName('Dim', 'Sub', count)@=soughtElement);
SubElemExists=1;
endif;
end;
# If element wasn't found in the subset already, then add it in&#46;
if(SubElemExists)=0;
SubsetElementInsert('Dim', 'sub', soughtElement, 1);
endif;

This means that ONLY the last element of the list is added to the subset. Still not quite right.

Re: Subset Element Exists

Posted: Fri Sep 19, 2008 7:43 pm
by Alan Kirk
MattyG wrote:Moving up count = count + 1 :

top=SubsetGetSize('Dim', 'Sub');
count=1;
SubElemExists=0;
while(count<top);
count=count+1;
if(SubsetGetElementName('Dim', 'Sub', count)@=soughtElement);
SubElemExists=1;
endif;
end;
# If element wasn't found in the subset already, then add it in&#46;
if(SubElemExists)=0;
SubsetElementInsert('Dim', 'sub', soughtElement, 1);
endif;

This means that ONLY the last element of the list is added to the subset. Still not quite right.
I don't think that you should have moved
count=count+1;
up there; it means that you're ignoring the 1st element in the subset.

(Also 1 minor tweak that I'd suggest;

Code: Select all

if(SubsetGetElementName('Dim', 'Sub', count)@=soughtElement);
SubElemExists=1;
count = top;
endif;
There's no point looping through the other elements if you find the one you're after.

But as to your actual problem... you ARE doing this on the Metadata tab and not one of the others, yes?

Re: Subset Element Exists

Posted: Sat Sep 20, 2008 8:35 am
by MattyG
Hi Alan,

This is the code as it stands, which does what I want it to do. Moving the Count = Count + 1 up just puts the last element in the list into the subset.

I have put + 1 on the end of Top = SUBSETGETSIZE ( ' Dim ', ' Sub ' ), because without it the last element in the list is added twice into the subset.

I was doing all this on the Data tab, but I've just tried moving it to the Metadata tab, and the results are the same.

IF ( SUBSETEXISTS ( ' Dim ', ' Sub ' ) = 0 ) ;
SUBSETCREATE ( ' Dim ', ' Sub ' ) ;
ENDIF ;

Top = SUBSETGETSIZE ( ' Dim ', ' Sub ' ) + 1 ;
Count = 1 ;
SubsetElementExists = 0 ;
WHILE ( Count < Top ) ;
IF ( SUBSETGETELEMENTNAME ( ' Dim ', ' Sub ', Count ) @= SoughtElement) ;
SubsetElementExists = 1 ;
Count = Top ;
ENDIF ;
Count = Count + 1 ;
END ;
IF ( SubsetElementExists = 0 ) ;
SUBSETELEMENTINSERT ( ' Dim ', ' Sub ', SoughtElement, 1 ) ;
ENDIF ;

However, I would still like to incorporate the functionality of rebuilding the subset each time the process is run. Any ideas?

Cheers,
Matt