Page 1 of 1

Loop with different variables

Posted: July 19th, 2018, 3:26 pm
by charles.tor
Hi, I'm trying to write a loop that simplifies the following:

Code: Select all

	if pos("A", S1P08) <> 0 then
		S1P08A_1 = 1;
	else
		S1P08A_1 = 0;
	endif;	 

	if pos("B", S1P08) <> 0 then
		S1P08A_2 = 1;
	else
		S1P08A_2 = 0;
	endif;
	
	if pos("C", S1P08) <> 0 then
		S1P08A_3 = 1;
	else
		S1P08A_3 = 0;
	endif;
I have tried a solution like this:

Code: Select all

numeric i;
string alphabet = "ABCDEFGH";
do i = 1 while i <= 8
	string letter = alphabet[i:1];
	if pos(letter, S1P08) <> 0 then
		S1P08A_i = 1;
	else
		S1P08A_i = 0;
	endif;	
enddo;
But it doesn't work. I would appreciate any suggestions. I didn't define S1P08A as a single item with multiple occurences because S1P08A is already in a roster with multiple occurences.

Re: Loop with different variables

Posted: July 22nd, 2018, 7:19 am
by aaronw
I would rename S1P08A_i, and give it eight occurrences for each letter in your alphabet.
  • 1. S1P08Results(1) would be the results for A
    2. S1P08Results(2) would be the results for B
    8. S1P08Results(8) would be the results for H
Then the following should work:
numeric i;
string alphabet = "ABCDEFGH";
do i = 1 while i <= 8
   string letter = alphabet[i:1];
   if pos(letter, S1P08) <> 0 then
      S1P08Results(i) = 1;
   else
      S1P08Results(i) = 0;
   endif;
enddo;