cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
  • Learn how to build custom Python data connectors and further customize JMP’s Data Connector Framework with the Python Data Connector Demo, available now in the JMP Marketplace!
  • See how to create experiments to support product design and ID useful product features. Register for June 12 webinar, 2pm US Eastern Time.

Discussions

Solve problems, and share tips and tricks with other JMP users.
Choose Language Hide Translation Bar

How to parse commas within a List Box

Hello,

I am on JMP 18.1.2 and am attempting to create a matrix of check boxes. The final step which I cannot figure out, is how to dynamically create the H List Box based on number of rows.

This is my current code:

test = {"a","b","c","d","e"};
testFin = {};
rows = 2;
remain = Modulo(N Items(test), rows);
loops = 2 + (remain > 0);
//create a list that groups the original list in groups of 2 (rows)
for(i=1, i<= loops, i++,
	test2 = {};
	if(i != loops,
		for(j = 1, j<= rows, j++,
			test2[j] = test[1];
			test = remove(test,1);
		);
		,
		for(j = 1, j<= remain, j++,
			test2[j] = test[1];
			test = remove(test,1);
		);
	);
	testFin[i] = test2;
);

//testFin = {{"a", "b"}, {"c", "d"}, {"e"}}
cb = {};
cancel_check = 0;
ttp = "";
for(i = 1, i<= N Items(testFin), i++,
	if(i != N Items(testFin),
		ttp ||= "cb["||Char(i)||"] = Check Box(testFin["||Char(i)||"]), \!n"
		,
		ttp ||= "cb["||Char(i)||"] = Check Box(testFin["||Char(i)||"]);"
	);
);
/*ttp = "cb[1] = Check Box(testFin[1]), 
cb[2] = Check Box(testFin[2]), 
cb[3] = Check Box(testFin[3]);"*/
testw = new window("raaa", <<Modal,
	H List Box(
		parse(ttp);
	),
	H List Box(
		Button Box("OK",
			//testing
			cbtemp = cb[1] << Get Selected();
		),
		Button Box("Cancel", cancel_check = 1);
	);
);
if(cancel_check == 1,
	Stop()
);

The error I get when trying to run it is:

Unexpected ",". Perhaps there is a missing ";" or ",".
Line 1 Column 30: cb[1] = Check Box(testFin[1])►, 

The remaining text that was ignored was
,cb[2]=Check Box(testFin[2]),cb[3]=Check Box(testFin[3]);

 

The desired output (if I just replaced the parse with what ttp is and ran it) is attached and also below.
Thank you!

_________________

Desired OutputDesired Output

1 ACCEPTED SOLUTION

Accepted Solutions
jthi
Super User

Re: How to parse commas within a List Box

I would avoid Parse() as it can be a nightmare to debug and it will teach you horrible ways of writing JSL script. I would attempt to do something like this or something similar which would avoid using Parse()

Names Default To Here(1);

test = {"a", "b", "c", "d", "e"};
testFin = {};
rows = 2;
remain = Modulo(N Items(test), rows);
loops = 2 + (remain > 0);

//create a list that groups the original list in groups of 2 (rows)
For(i = 1, i <= loops, i++,
	test2 = {};
	If(i != loops,
		For(j = 1, j <= rows, j++,
			test2[j] = test[1];
			test = Remove(test, 1);
		)
	,
		For(j = 1, j <= remain, j++,
			test2[j] = test[1];
			test = Remove(test, 1);
		)
	);
	testFin[i] = test2;
);

cancel_check = 0;
cbs = {};
hlb = H List Box();
For(i = 1, i <= N Items(testFin), i++,
	hlb << Append(cbs[i] = Check Box(testFin[i]));
);

testw = New Window("raaa",
	<<Modal,
	hlb,
	H List Box(
		Button Box("OK", 
			//testing
			cbtemp = cb[1] << Get Selected()
		),
		Button Box("Cancel", cancel_check = 1)
	)
);

If(cancel_check == 1, Stop());
-Jarmo

View solution in original post

2 REPLIES 2
txnelson
Super User

Re: How to parse commas within a List Box

I changed up your script a bit.  I continued using your methodology of building a string that contains the elements required to display the Check Boxes.

I also used

     Eval( Eval Expr())

To force the evaluation of the string before executing it.

txnelson_0-1745890770707.png

Names Default To Here( 1 );
test = {"a", "b", "c", "d", "e"};
testFin = {};
rows = 2;
remain = Modulo( N Items( test ), rows );
loops = 2 + (remain > 0);
//create a list that groups the original list in groups of 2 (rows)
For( i = 1, i <= loops, i++,
	test2 = {};
	If( i != loops,
		For( j = 1, j <= rows, j++,
			test2[j] = test[1];
			test = Remove( test, 1 );
		)
	,
		For( j = 1, j <= remain, j++,
			test2[j] = test[1];
			test = Remove( test, 1 );
		)
	);
	testFin[i] = test2;
);

cb = {};
cancel_check = 0;
ttp = "lineupBox(ncol(" || Char( N Items( testFin ) ) || "),";
For( k = 1, k <= 2, k++,
	For( i = 1, i <= N Items( testFin ), i++,
		Try( ttp ||= "cb[" || Char( (k - 1) * N Items( testfin ) + i ) || "] =  Check Box(\!"" || testFin[i][k] || "\!")," )
	)
);
ttp = Substr( ttp, 1, Length( ttp ) - 1 ) || ")";

Eval(
	Eval Expr(
		testw = New Window( "raaa",
			<<Modal,
			Expr( Parse( ttp ) ),
			H List Box(
				Button Box( "OK", 
			//testing
					cbtemp = cb[1] << Get Selected()
				),
				Button Box( "Cancel", cancel_check = 1 )
			)
		)
	)
);
Jim
jthi
Super User

Re: How to parse commas within a List Box

I would avoid Parse() as it can be a nightmare to debug and it will teach you horrible ways of writing JSL script. I would attempt to do something like this or something similar which would avoid using Parse()

Names Default To Here(1);

test = {"a", "b", "c", "d", "e"};
testFin = {};
rows = 2;
remain = Modulo(N Items(test), rows);
loops = 2 + (remain > 0);

//create a list that groups the original list in groups of 2 (rows)
For(i = 1, i <= loops, i++,
	test2 = {};
	If(i != loops,
		For(j = 1, j <= rows, j++,
			test2[j] = test[1];
			test = Remove(test, 1);
		)
	,
		For(j = 1, j <= remain, j++,
			test2[j] = test[1];
			test = Remove(test, 1);
		)
	);
	testFin[i] = test2;
);

cancel_check = 0;
cbs = {};
hlb = H List Box();
For(i = 1, i <= N Items(testFin), i++,
	hlb << Append(cbs[i] = Check Box(testFin[i]));
);

testw = New Window("raaa",
	<<Modal,
	hlb,
	H List Box(
		Button Box("OK", 
			//testing
			cbtemp = cb[1] << Get Selected()
		),
		Button Box("Cancel", cancel_check = 1)
	)
);

If(cancel_check == 1, Stop());
-Jarmo

Recommended Articles