概要
場合によっては、同じタイプの複数のオブジェクトに対して同じタスクを実行する必要があります。 1 つの方法は、各オブジェクトに対してステートメントを繰り返すことです。 ただし、この方法では、各オブジェクトに対してステートメントを再入力するか、ループを使用する必要があります。
便利な方法は、オブジェクトのリストにメッセージを送信することです。この方法では、メッセージを 1 回だけ入力する必要があります。以下に 2 つの例を示します。
例1
2 つのデータ テーブルがあり、各テーブルに同じ数式列を追加したいとします。また、各テーブルを新しいテーブルに分類し、新しいテーブルへの参照を作成したいとします。次に示すように、テーブルのリストにメッセージを送信したり、等号の左側に名前のリストを使用したりすることができます。
//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
複数の TextBox があり、それらすべてのフォントの色を変更し、テキストを取得してその情報を新しい変数に格納するとします。ここで示すように、リストを使用して一度にすべてのアクションを実行できます。
//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]