Loop with different variables

Discussions about CSEntry
Forum rules
New release: CSPro 8.0
Post Reply
charles.tor
Posts: 2
Joined: September 1st, 2017, 7:09 pm

Loop with different variables

Post 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.
aaronw
Posts: 561
Joined: June 9th, 2016, 9:38 am
Location: Washington, DC

Re: Loop with different variables

Post 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;
Post Reply