cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Try the Materials Informatics Toolkit, which is designed to easily handle SMILES data. This and other helpful add-ins are available in the JMP® Marketplace
%3CLINGO-SUB%20id%3D%22lingo-sub-622488%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3EBetreff%3A%20JSL-Skript%20zum%20Erstellen%20oder%20Bedingen%20durch%20Schleifen%20vollst%C3%A4ndiger%20Spaltennamen%20in%20einer%20Liste%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-622488%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EHier%20ist%20ein%20Beispiel%20f%C3%BCr%20einen%20Weg%2C%20es%20zu%20tun%20...%20andere%20haben%20vielleicht%20eloquentere%20Wege%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3ENames%20Default%20To%20Here(%201%20)%3B%0Adt%20%3D%20%0A%2F%2F%20Open%20Data%20Table%3A%20Big%20Class.jmp%0A%2F%2F%20%E2%86%92%20Data%20Table(%20%22Big%20Class%22%20)%0AOpen(%20%22%24SAMPLE_DATA%2FBig%20Class.jmp%22%20)%3B%0A%0Amy_func%20%3D%20Function(%20%7Binput_dt%2C%20input_col_list%2C%20x%7D%2C%0A%20%7Binput_dt%2C%20input_col_list%2C%20x%2C%20the_cmd%7D%2C%0A%20input_dt%20%26lt%3B%26lt%3B%20New%20Column(%20%22d%22%20)%3B%0A%20current%20data%20table(input_dt)%3B%0A%20the_cmd%20%3D%20%22current%20data%20table()%3Ad%20%26lt%3B%26lt%3B%20set%20formula(%20If(%20%20current%20data%20table()%3A%5C!%22%22%20%7C%7C%0A%20Char(%20input_col_list%5B1%5D%20)%20%7C%7C%20%22%5C!%22n%20%26gt%3B%3D%20%22%20%7C%7C%20Char(%20x%20)%3B%0A%20For(%20i%20%3D%202%2C%20i%20%26lt%3B%3D%20N%20Items(%20input_col_list%20)%2C%20i%2B%2B%2C%0A%20%20the_cmd%20%3D%20the_cmd%20%7C%7C%20%22%20%7C%20current%20data%20table()%3A%5C!%22%22%20%7C%7C%20Char(%20input_col_list%5Bi%5D%20)%20%7C%7C%20%22%5C!%22n%20%26gt%3B%3D%20%22%0A%20%20%20%7C%7C%20Char(%20x%20)%0A%20)%3B%0A%20the_cmd%20%3D%20the_cmd%20%7C%7C%20%22%2C%201%2C%200)%20)%3B%22%3B%0A%20Eval(%20Parse(%20the_cmd%20)%20)%3B%0A)%3B%0A%0Ainput_col_listx%20%3D%20%7B%22Height%22%2C%20weight%7D%3B%0Adtx%20%3D%20Data%20Table(%20%22big%20class%22%20)%3B%0Axx%20%3D%20101%3B%0A%0AtheReturn%20%3D%20my_func(%20dtx%2C%20input_col_listx%2C%20xx%20)%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-622489%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3EBetreff%3A%20JSL-Skript%20zum%20Erstellen%20oder%20Bedingen%20durch%20Schleifen%20vollst%C3%A4ndiger%20Spaltennamen%20in%20einer%20Liste%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-622489%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EDas%20Erstellen%20mit%20Zeichenfolgen%20ist%20h%C3%B6chstwahrscheinlich%20die%20einfachste%20Option%2C%20da%20der%20Ausdruck%20recht%20einfach%20ist.%20Aber...%20Ich%20mag%20es%20nicht%2C%20Eval%20(Parse%20())%20zu%20verwenden%2C%20also%20sind%20hier%20einige%20Optionen%2C%20die%20das%20nicht%20verwenden%20(die%20zweite%20Option%20tut%20m%C3%B6glicherweise%20nicht%20das%2C%20was%20Sie%20mit%20fehlenden%20Werten%20wollen).%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3ENames%20Default%20To%20Here(1)%3B%0A%0Adt%20%3D%20New%20Table(%22Untitled%203%22%2C%0A%20Add%20Rows(0)%2C%0A%20Compress%20File%20When%20Saved(1)%2C%0A%20New%20Column(%22a%22%2C%20Numeric%2C%20%22Continuous%22%2C%20Format(%22Best%22%2C%2012)%2C%20Set%20Values(%5B0%2C1%5D))%2C%0A%20New%20Column(%22b%22%2C%20Numeric%2C%20%22Continuous%22%2C%20Format(%22Best%22%2C%2012)%2C%20Set%20Values(%5B0%2C0%5D))%2C%0A%20New%20Column(%22c%22%2C%20Numeric%2C%20%22Continuous%22%2C%20Format(%22Best%22%2C%2012)%2C%20Set%20Values(%5B0%2C0%5D))%0A)%3B%0Ainput_col_list%20%3D%20%7B%22a%22%2C%20%22b%22%2C%20%22c%22%7D%3B%0Ax%20%3D%200.1%3B%0A%0A%2F%2F%20using%20Or%0Aor_expr%20%3D%20Expr(Or())%3B%0A%2F%2F%20https%3A%2F%2Fcommunity.jmp.com%2Ft5%2FJSL-Cookbook-Archived%2FInsert-one-expression-into-another-using-Eval-Insert-Eval-Expr%2Fta-p%2F48998%0AFor%20Each(%7Bcol_name%7D%2C%20input_col_list%2C%0A%20col_expr%20%3D%20Substitute(%0A%20%20expr(_col_ref_%20%26gt%3B%3D%20_comparison_value_)%2C%0A%20%20Expr(_col_ref_)%2C%20NameExpr(AsColumn(Column(dt%2C%20col_name)))%2C%0A%20%20Expr(_comparison_value_)%2C%20x%0A%20)%3B%0A%20Insert%20Into(or_expr%2C%20Name%20Expr(col_expr))%3B%0A)%3B%0A%2F%2F%20Show(name%20expr(or_expr))%3B%0AEval(EvalExpr(dt%20%26lt%3B%26lt%3B%20New%20Column(%22d%22%2C%20Numeric%2C%20Continuous%2C%20Formula(%0A%20If(Expr(name%20expr(or_expr))%2C%201%2C%200)%0A))))%3B%0A%0A%0A%2F%2F%20using%20matrix%20and%20any%0Acol_refs%20%3D%20Transform%20Each(%7Bcol_name%7D%2C%20input_col_list%2C%0A%20Name%20Expr(As%20Column(Column(dt%2C%20col_name)))%3B%0A)%3B%0AEval(EvalExpr(dt%20%26lt%3B%26lt%3B%20New%20Column(%22d%22%2C%20Numeric%2C%20Continuous%2C%20Formula(%0A%20Any(Matrix(Expr(col_refs))%20%26gt%3B%3D%20Expr(x))%0A))))%3B%0A%0A%0A%2F%2F%20function%20of%20second%20option%0Acreate_check_col%20%3D%20function(%7Bdt%2C%20col_input%2C%20limit_val%7D%2C%20%7BDefault%20Local%7D%2C%0A%20col_refs%20%3D%20Transform%20Each(%7Bcol_name%7D%2C%20col_input%2C%0A%20%20Name%20Expr(As%20Column(Column(dt%2C%20col_name)))%3B%0A%20)%3B%0A%20%0A%20new_col%20%3D%20Eval(EvalExpr(dt%20%26lt%3B%26lt%3B%20New%20Column(%22d%22%2C%20Numeric%2C%20Continuous%2C%20Formula(%0A%20%20Any(Matrix(Expr(col_refs))%20%26gt%3B%3D%20Expr(limit_val))%0A%20))))%3B%0A%20%0A%20return(new_col)%3B%0A)%3B%0Acreate_check_col(dt%2C%20input_col_list%2C%20x)%3B%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-622464%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3EJSL-Skript%20zum%20Erstellen%20oder%20Konditionieren%20durch%20Durchlaufen%20vollst%C3%A4ndiger%20Spaltennamen%20in%20einer%20Liste%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-622464%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EIch%20m%C3%B6chte%20in%20jsl%20eine%20Funktion%20erstellen%2C%20die%20eine%20bestimmte%20Liste%20von%20Spaltennamen%20als%20Eingabe%20verwendet%2C%20diese%20Spalten%20verwendet%2C%20um%20eine%20ODER-Bedingung%20auszuf%C3%BChren%2C%20und%20eine%20Spalte%20mit%20dieser%20Schleifen-OR-Formel%20ausgibt.%20Wie%20machst%20du%20das%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3Einput_col_list%20%3D%20%7B%20%22a%22%2C%20%22b%22%2C%20%22c%22%7D%3B%0Ainput_dt%20%3D%20dt%3B%0Ax%20%3D%200.1%3B%0Ainput_dt%20%26lt%3B%26lt%3B%20New%20Column(%22d%22%2C%20numeric%2C%20continuous%2C%20%0Aformula%20(%20%0AIf(%0A(As%20Column(%22a%22)%20%26gt%3B%3D%20x%20%20%7C%20%20As%20Column(%22b%22)%20%26gt%3B%3D%20x%20%20%7C%20As%20Column(%22c%22)%20%26gt%3B%3D%20%20x)%2C%20%0A1%2C%0A0%0A)%20%0A))%3B%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EF%C3%BCr%20oben%20m%C3%B6chte%20ich%20eine%20Funktion%20wie%20die%20folgende%20erstellen%3A%3C%2FP%3E%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3Emy_func%20%3D%0AFunction(%0A%7Binput_dt%2C%20input_col_list%2C%20x%7D%2C%0A%7B%2F%2Flocal%20var%7D%2C%0A%2F%2F%20function%20body%0A...%0AReturn%20As%20column(%22d%22)%0A)%3B%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-622464%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CLINGO-LABEL%3EQualit%C3%A4ts-%20und%20Verfahrenstechnik%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Choose Language Hide Translation Bar

JSL script to create or condition by looping thorough column names in a list

I'd like to create a function in jsl that takes a given list of column names as input, uses those columns to do a OR condition, and outputs a column with that looping OR formula. How do U do that? 

 

input_col_list = { "a", "b", "c"};
input_dt = dt;
x = 0.1;
input_dt << New Column("d", numeric, continuous, 
formula ( 
If(
(As Column("a") >= x  |  As Column("b") >= x  | As Column("c") >=  x), 
1,
0
) 
));

For above, I would like to make a function like below: 

my_func =
Function(
{input_dt, input_col_list, x},
{//local var},
// function body
...
Return As column("d")
);
1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: JSL script to create or condition by looping thorough column names in a list

Here is an example of one way to do it.......others may have more eloquent ways

Names Default To Here( 1 );
dt = 
// Open Data Table: Big Class.jmp
// → Data Table( "Big Class" )
Open( "$SAMPLE_DATA/Big Class.jmp" );

my_func = Function( {input_dt, input_col_list, x},
	{input_dt, input_col_list, x, the_cmd},
	input_dt << New Column( "d" );
	current data table(input_dt);
	the_cmd = "current data table():d << set formula( If(  current data table():\!"" ||
	Char( input_col_list[1] ) || "\!"n >= " || Char( x );
	For( i = 2, i <= N Items( input_col_list ), i++,
		the_cmd = the_cmd || " | current data table():\!"" || Char( input_col_list[i] ) || "\!"n >= "
		 || Char( x )
	);
	the_cmd = the_cmd || ", 1, 0) );";
	Eval( Parse( the_cmd ) );
);

input_col_listx = {"Height", weight};
dtx = Data Table( "big class" );
xx = 101;

theReturn = my_func( dtx, input_col_listx, xx );

 

Jim

View solution in original post

2 REPLIES 2
txnelson
Super User

Re: JSL script to create or condition by looping thorough column names in a list

Here is an example of one way to do it.......others may have more eloquent ways

Names Default To Here( 1 );
dt = 
// Open Data Table: Big Class.jmp
// → Data Table( "Big Class" )
Open( "$SAMPLE_DATA/Big Class.jmp" );

my_func = Function( {input_dt, input_col_list, x},
	{input_dt, input_col_list, x, the_cmd},
	input_dt << New Column( "d" );
	current data table(input_dt);
	the_cmd = "current data table():d << set formula( If(  current data table():\!"" ||
	Char( input_col_list[1] ) || "\!"n >= " || Char( x );
	For( i = 2, i <= N Items( input_col_list ), i++,
		the_cmd = the_cmd || " | current data table():\!"" || Char( input_col_list[i] ) || "\!"n >= "
		 || Char( x )
	);
	the_cmd = the_cmd || ", 1, 0) );";
	Eval( Parse( the_cmd ) );
);

input_col_listx = {"Height", weight};
dtx = Data Table( "big class" );
xx = 101;

theReturn = my_func( dtx, input_col_listx, xx );

 

Jim
jthi
Super User

Re: JSL script to create or condition by looping thorough column names in a list

Building this using strings is most likely most simple option as the expression is quite simple one. But... I don't like using Eval(Parse()) so here are few options which are not using that (second option might not do what you want with missing values)

Names Default To Here(1);

dt = New Table("Untitled 3",
	Add Rows(0),
	Compress File When Saved(1),
	New Column("a", Numeric, "Continuous", Format("Best", 12), Set Values([0,1])),
	New Column("b", Numeric, "Continuous", Format("Best", 12), Set Values([0,0])),
	New Column("c", Numeric, "Continuous", Format("Best", 12), Set Values([0,0]))
);
input_col_list = {"a", "b", "c"};
x = 0.1;

// using Or
or_expr = Expr(Or());
// https://community.jmp.com/t5/JSL-Cookbook-Archived/Insert-one-expression-into-another-using-Eval-Insert-Eval-Expr/ta-p/48998
For Each({col_name}, input_col_list,
	col_expr = Substitute(
		expr(_col_ref_ >= _comparison_value_),
		Expr(_col_ref_), NameExpr(AsColumn(Column(dt, col_name))),
		Expr(_comparison_value_), x
	);
	Insert Into(or_expr, Name Expr(col_expr));
);
// Show(name expr(or_expr));
Eval(EvalExpr(dt << New Column("d", Numeric, Continuous, Formula(
	If(Expr(name expr(or_expr)), 1, 0)
))));


// using matrix and any
col_refs = Transform Each({col_name}, input_col_list,
	Name Expr(As Column(Column(dt, col_name)));
);
Eval(EvalExpr(dt << New Column("d", Numeric, Continuous, Formula(
	Any(Matrix(Expr(col_refs)) >= Expr(x))
))));


// function of second option
create_check_col = function({dt, col_input, limit_val}, {Default Local},
	col_refs = Transform Each({col_name}, col_input,
		Name Expr(As Column(Column(dt, col_name)));
	);
	
	new_col = Eval(EvalExpr(dt << New Column("d", Numeric, Continuous, Formula(
		Any(Matrix(Expr(col_refs)) >= Expr(limit_val))
	))));
	
	return(new_col);
);
create_check_col(dt, input_col_list, x);
-Jarmo