cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
dadawasozo
Level IV

no output on column formula with For loop and if() to count repeated str in cells

Hi, Below (section A) is my code for a formula in a new column and it is working. However, I want to use for loop to replace it. I tried on it(Section B) , but there is not output at all. I wonder if someone can give me some advice of my mistake on section B? the example of column smap1 is in attached table. Thanks
section A:

dt3 << new column("smap", character,
formula(	

	If(N Rows( Loc( Words( :smap1, "," ), "0" ) ) >= 3, "0:0:0:0:0:0:0:0",
	   N Rows( Loc( Words( :smap1, "," ), "1" ) ) >= 3, "1:1:1:1:1:1:1:1",
	   N Rows( Loc( Words( :smap1, "," ), "2" ) ) >= 3, "2:2:2:2:2:2:2:2",
	   N Rows( Loc( Words( :smap1, "," ), "3" ) ) >= 3, "3:3:3:3:3:3:3:3",
	   N Rows( Loc( Words( :smap1, "," ), "4" ) ) >= 3, "4:4:4:4:4:4:4:4",
	   N Rows( Loc( Words( :smap1, "," ), "5" ) ) >= 3, "5:5:5:5:5:5:5:5",
	   N Rows( Loc( Words( :smap1, "," ), "6" ) ) >= 3, "6:6:6:6:6:6:6:6",
	   N Rows( Loc( Words( :smap1, "," ), "7" ) ) >= 3, "7:7:7:7:7:7:7:7",
	   "0:1:2:3:4:5:6:7"
		
	);
);
);

section B: 

dt3 << new column("smap", character,
formula(	

map={"0","1","2","3","4","5","6","7"};

for (l=1, l<=N items(map), l++,
	A=":"||map[l];
	B=map[l];
	If(N Rows( Loc( Words( :smap1, "," ), char(map[l])) )>= 3, concat(B,A,A,A,A,A,A,A),
	"0:1:2:3:4:5:6:7"
);
)
)
);
1 ACCEPTED SOLUTION

Accepted Solutions
ErraticAttack
Level VI

Re: no output on column formula with For loop and if() to count repeated str in cells

Your formula is returning an Empty() value.  The For() loop exits when l = 9.  The logic is that when l = 8 the loop returns to the incrementor then the test.  The test for l <= N Items( map ) fails, returning an empty value.  What you need to do is to terminate the loop at the point the If() statement is true, then return the result.  Here is an example:

 

dt3 << New Column( "smap",
	character,
	formula(	
		res = "0:1:2:3:4:5:6:7";
		For( l = 0, l < 8, l++,
			If( N Rows( Loc( Words( :smap1, "," ), Char( l ) ) ) >= 3,
				res = Concat Items( Repeat( Insert( {}, Char( l ) ), 8 ), ":" );
				Break();
			);
		);
		res;
	)
);

 

Jordan

View solution in original post

1 REPLY 1
ErraticAttack
Level VI

Re: no output on column formula with For loop and if() to count repeated str in cells

Your formula is returning an Empty() value.  The For() loop exits when l = 9.  The logic is that when l = 8 the loop returns to the incrementor then the test.  The test for l <= N Items( map ) fails, returning an empty value.  What you need to do is to terminate the loop at the point the If() statement is true, then return the result.  Here is an example:

 

dt3 << New Column( "smap",
	character,
	formula(	
		res = "0:1:2:3:4:5:6:7";
		For( l = 0, l < 8, l++,
			If( N Rows( Loc( Words( :smap1, "," ), Char( l ) ) ) >= 3,
				res = Concat Items( Repeat( Insert( {}, Char( l ) ), 8 ), ":" );
				Break();
			);
		);
		res;
	)
);

 

Jordan