概述
有时需要对同一类型的多个对象执行相同的任务。一种方法是针对每个对象重复该语句。但是,这要求您为每个对象重新输入语句或使用循环。
一种方便的方法是将消息发送给对象列表。此方法要求您仅输入一次消息。下面给出了两个示例。
示例 1
假设我有两个数据表,我想向每个表添加相同的公式列。此外,假设我想将每个表排序为新表并创建对新表的引用。您可以将消息发送到表列表(如下所示),也可以使用等号左侧的名称列表。
//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 ) );
示例 2
假设我有多个文本框,我想更改所有文本框的字体颜色,同时检索文本并将信息存储到新变量中。如下所示,您可以使用列表一次性执行所有操作。
//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;
[先前的 JMP 说明 50975]