BookmarkSubscribe
Choose Language Hide Translation Bar
john_madden
Community Trekker

Help with column variables

Hi all,

I am having trouble understanding why the following code fails:

// This code doesn't work

dt = Current Data Table();
selectedCol = dt << Get Selected Columns();

If( Col Mean( selectedCol[1] ) < Col Mean( selectedCol[2] ), 

	begin = selectedCol[1];
	end = selectedCol[2];
,
	begin = selectedCol[2];
	end = selectedCol[1];
);

I want the whole column reassigned to the begin/end variables, but it doesn't happen. I tried this:

// This code still doesn't work

dt = Current Data Table();
selectedCol = dt << Get Selected Columns();

If( Col Mean( As Column( selectedCol[1] ) ) < Col Mean( As Column( selectedCol[2] ) ), 

	begin = As Column( selectedCol[1] );
	end = As Column( selectedCol[2] );
,
	begin = As Column( selectedCol[2] );
	end = As Column( selectedCol[1] );
);

Can I get some help, conceptual and practical both. This seems really simple, but I'm obviously missing something important.

 

John

0 Kudos
1 ACCEPTED SOLUTION

Accepted Solutions
julian
Staff

Re: Help with column variables

Hi @john_madden,

Two things I notice here. First, JSL evaluates nested functions from the outside in, meaning you need an Eval() for the reference to the column that is part of your list. Second, if I'm understanding you correctly, you want all the column values assigned to begin and end. If so, you need to send a message to the column to return those values: << Get Values. The following should work (assuming that's your intent!). 

 

dt = Current Data Table();
selectedCol = dt << Get Selected Columns();

If( Col Mean( eval(selectedCol[1]) ) < Col Mean( eval(selectedCol[2]) ), 

	begin = selectedCol[1] << Get Values;
	end = selectedCol[2] << Get Values;
,
	begin = selectedCol[2] << Get Values;
	end = selectedCol[1] << Get Values;
);

I hope this helps!

@julian

 

 

Edit: I can see how someone might look at my point about nested functions and say "Wait, Col Mean( selectedCol[1] )' isn't a nested function, so why doesn't that work?" In fact, subscribing is a function, it's just done conveniently with brackets. You could have written: 

Col Mean( Subscript(selectedCol,1) )

and in this case, the nesting of the function is obvious. What happens when JMP tries to evaluate this bit of code is that Col Mean() is evaluated first, and that function requires a column to operate on. It hits a Subscript() function and doesn't know what to do with it. So, by wrapping Subscript() in Eval() we tell JMP to first process the subscripting (which returns a reference to a column) before it tries to evaluate Col Mean(). 

Col Mean( Eval( Subscript( selectedCol,1 ) ) )

I hope the additional explanation helps!

 

2 REPLIES 2
julian
Staff

Re: Help with column variables

Hi @john_madden,

Two things I notice here. First, JSL evaluates nested functions from the outside in, meaning you need an Eval() for the reference to the column that is part of your list. Second, if I'm understanding you correctly, you want all the column values assigned to begin and end. If so, you need to send a message to the column to return those values: << Get Values. The following should work (assuming that's your intent!). 

 

dt = Current Data Table();
selectedCol = dt << Get Selected Columns();

If( Col Mean( eval(selectedCol[1]) ) < Col Mean( eval(selectedCol[2]) ), 

	begin = selectedCol[1] << Get Values;
	end = selectedCol[2] << Get Values;
,
	begin = selectedCol[2] << Get Values;
	end = selectedCol[1] << Get Values;
);

I hope this helps!

@julian

 

 

Edit: I can see how someone might look at my point about nested functions and say "Wait, Col Mean( selectedCol[1] )' isn't a nested function, so why doesn't that work?" In fact, subscribing is a function, it's just done conveniently with brackets. You could have written: 

Col Mean( Subscript(selectedCol,1) )

and in this case, the nesting of the function is obvious. What happens when JMP tries to evaluate this bit of code is that Col Mean() is evaluated first, and that function requires a column to operate on. It hits a Subscript() function and doesn't know what to do with it. So, by wrapping Subscript() in Eval() we tell JMP to first process the subscripting (which returns a reference to a column) before it tries to evaluate Col Mean(). 

Col Mean( Eval( Subscript( selectedCol,1 ) ) )

I hope the additional explanation helps!

 

Highlighted
john_madden
Community Trekker

Re: Help with column variables

Thank you, thank you Julian!

 

That is incredibly helpful !!!!

 

John