cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
  • Register to attend Discovery Summit 2025 Online: Early Users Edition, Sept. 24-25.
  • New JMP features coming to desktops everywhere this September. Sign up to learn more at jmp.com/launch.
Choose Language Hide Translation Bar

JSL script to check if a single cell value is in a list

Given a data table with a column of limited values (se examples below), I would like to write a script that looks at the first cell and checks if it is in a list of values. The purpose would be to use whatever that first value as the conditoin for an IF statement that would then point to a certain set of tasks.

 

Anyone have an idea how to do this?

thanks

 

e.g.

CATtime
A1
B2
C3
D4
E5

or

CATtime
V1
W2
X3
Y4
Z5
5 REPLIES 5
txnelson
Super User

Re: JSL script to check if a single cell value is in a list

I am not sure exactly what you are asking for, however, below is a script that might point you in the right direction

Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/big class.jmp" );

theList = {59, 66};

For( i = 1, i <= N Rows( dt ), i++,
	If( N Rows( Loc( theList, :height[i] ) ) > 0,
		Show( i )
	)
);
Jim
pmroz
Super User

Re: JSL script to check if a single cell value is in a list

This might be helpful:

dt = New Table( "test", Add Rows( 5 ),
	New Column( "CAT", Character, "Nominal",
		Set Values( {"A", "B", "C", "D", "E"} ) ),
	New Column( "time", Numeric, "Continuous", Format( "Best", 12 ),
		Set Values( [1, 2, 3, 4, 5] ) )
);
look_for_list = {"B", "D"};
found_rows = dt << get rows where(contains(look_for_list, as column(dt, "CAT")));
print(found_rows);
[2, 4]

Re: JSL script to check if a single cell value is in a list

Thanks @pmroz and @txnelson for the scripting help. To be more specific, what I am looking for is to use the check of whether or not the value of the first cell is in the list as the conditions for an if statement which will drive some other actions. I think that both of your scripts fill another array but they're not a strict T/F. I will try to use them and let you know what happens.

Plotasaurus
Level II

Re: JSL script to check if a single cell value is in a list

Came across this thread looking for something similar for closing lingering open data tables while testing code. Used your simple code above. Thought I'd post in case it's useful for anyone coming across this thread:

 

// list of table names that I want to make sure are closed before running the script
tname = { "TABLE1", "TABLE2", "TABLE3", "TABLE4" };

// loop to see if open tables match the name in the list above, then close them
// need to restart the i counter to i = 0 because it seems closing a table changes the 'i' number associated with the table
For( i = 1, i <= N Table(), i++,

	If( N Rows (Loc( tname, Data Table(i) << Get Name ) ) > 0, 
		Close(Data Table(i), no save);
		i = 0;
	)
);
hogi
Level XII

Re: JSL script to check if a single cell value is in a list

brute force variant:

For Each ({dt_name, { "TABLE1", "TABLE2", "TABLE3", "TABLE4" },
	Try(Close(Data Table(dt_name), no save) 	)
)

Recommended Articles