cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Using lists in JSL to perform the same task on multiple objects

Overview

Sometimes it is necessary to perform the same task on several objects of the same type. One approach is to repeat the statement for each object. However, this requires you to retype the statement for each object or use a loop.

A handy approach is to send the message to a list of objects. This method requires you to type the message only once. Two examples are given below.

 

Example 1

Suppose I have two data tables and I want to add the same formula column to each table. Also, suppose I want to sort each of the tables into new tables and create references to the new tables. You can send the messages to a list of the tables as shown below, as well as using a list of names on the left side of the equal sign.

//Create 2 sample tables
dt1 = New Table( "Table1",
	Add Rows( 10 ),
	New Column( "Index", "Numeric", "Ordinal", Set Values( Index( 1, 10 ) ) )
);
dt2 = New Table( "Table2",
	Add Rows( 10 ),
	New Column( "Index", "Numeric", "Ordinal", Set Values( Index( 11, 20 ) ) )
);

//Add a formula column to both tables
{dt1, dt2} << New Column( "Random", Formula( Random Normal() ) );

//Sort by the new column and return a list of references to the new sorted tables
{dt1s, dt2s} = {dt1, dt2} << Sort( By( :Random ) ); 

Example 2

Suppose I have several TextBoxes and I want to change the font color for all of them, and also retrieve the text and store the information into new variables. As shown here, you can use lists to perform the actions all at once.

//Create a window containing a few TextBoxes
nw = New Window( "Example",
	tb1 = Text Box( "This is text box 1" ),
	tb2 = Text Box( "This is text box 2" ),
	tb3 = Text Box( "This is text box 3" )
);
//Set the font color by sending the message to a list of the objects
{tb1, tb2, tb3} << Font Color( Red );

//Return a list of the text stored in each TextBox
{text1, text2, text3} = {tb1, tb2, tb3} << Get Text;

 

 

 

[Previously JMP Note 50975]

Details
Operating System
macOS Windows
Products JMP JMP Pro