公開日: 11/25/2021

連想配列の使用

キー数の検出

連想配列に含まれているキー数を調べるには、N Items()関数を使います。

cary = Associative Array();
cary["state"] = "NC";
cary["population"] = 116234;
cary["weather"] = "cloudy";
cary["population"] += 10;
cary["weather"] = "sunny";
cary["high schools"] = {"Cary", "Green Hope", "Panther Creek"};
N Items( cary );

4

キーおよび値の追加と削除

連想配列へのキーと値のペアの追加や削除には、次の関数を使用します。

Insert()

Insert Into()

Remove()

Remove From()

メモ:

Insert()Remove()は、キーと値のペアを追加または削除した連想配列のコピーを戻します。

Insert Into()Remove From()は、指定の連想配列に対して、直接キーと値のペアの追加または削除を行います。

Insert()Insert Into()は、連想配列、キー、および値の3つの引数を取ります。

Remove()Remove From()は、連想配列とキーの2つの引数を取ります。

値を指定しないでキーを挿入した場合は、キーには値1が割り当てられます。

次の例は、Insert()Insert Into()を説明しています。

cary = Associative Array();
cary["state"] = "NC";
cary["population"] = 116234;
cary["weather"] = "cloudy";
cary["population"] += 10;
cary["weather"] = "sunny";
cary["high schools"] = {"Cary", "Green Hope", "Panther Creek"};
newcary = Insert( cary, "time zone", "Eastern" );
Show( cary, newcary );

cary = ["high schools" => {"Cary", "Green Hope", "Panther Creek"}, "population" => 116244, "state" => "NC", "weather" => "sunny"];

newcary = ["high schools" => {"Cary", "Green Hope", "Panther Creek"}, "population" => 116244, "state" => "NC", "time zone" => "Eastern", "weather" => "sunny"];

 
cary = Associative Array();
cary["state"] = "NC";
cary["population"] = 116234;
cary["weather"] = "cloudy";
cary["population"] += 10;
cary["weather"] = "sunny";
cary["high schools"] = {"Cary", "Green Hope", "Panther Creek"};
Insert Into(cary, "county", "Wake");
Show( cary );

cary = ["county" => "Wake", "high schools" => {"Cary", "Green Hope", "Panther Creek"}, "population" => 116244, "state" => "NC", "weather" => "sunny"];

cary << Insertは連想配列に送られるメッセージで、Insert Into()関数と同じ処理を実行します。たとえば、次の2つのステートメントは同じ結果になります。

cary << Insert( "county", "Wake" );
Insert Into( cary, "county", "Wake" );

次の例は、Remove()Remove From()を説明しています。

cary = Associative Array();
cary["state"] = "NC";
cary["population"] = 116234;
cary["weather"] = "cloudy";
cary["population"] += 10;
cary["weather"] = "sunny";
cary["high schools"] = {"Cary", "Green Hope", "Panther Creek"};
newcary = Remove( cary, "high schools" );
Show( cary, newcary );

cary = ["high schools" => {"Cary", "Green Hope", "Panther Creek"}, "population" => 116244, "state" => "NC", "weather" => "sunny"];

newcary = ["population" => 116244, "state" => "NC", "weather" => "sunny"];

Remove From( cary, "weather" );
Show( cary );

cary = ["high schools" => {"Cary", "Green Hope", "Panther Creek"}, "population" => 116244, "state" => "NC", "weather" => "sunny"];

newcary = ["population" => 116244, "state" => "NC", "weather" => "sunny"];

cary = ["high schools" => {"Cary", "Green Hope", "Panther Creek"}, "population" => 116244, "state" => "NC"];

aa << Removeは連想配列に送られるメッセージで、Remove From()関数と同じ処理を実行します。たとえば、次の2つのステートメントは同じ結果になります。

cary << Remove( "weather" );
Remove From( cary, "weather" );

連想配列内のキーまたは値の検出

連想配列の中に特定のキーが含まれているかどうかを調べるには、Contains()を使用します。

cary = Associative Array();
cary["state"] = "NC";
cary["population"] = 116234;
cary["weather"] = "cloudy";
cary["population"] += 10;
cary["weather"] = "sunny";
cary["high schools"] = {"Cary", "Green Hope", "Panther Creek"};
Contains(cary, "high schools");

1

Contains(cary, "lakes");

0

連想配列に含まれるすべてのキーのリストを取得するには、<< Get Keysメッセージを使用します。

cary << Get Keys;

{"high schools", "population", "state", "weather"}

連想配列に含まれるすべての値のリストを取得するには、<< Get Valuesメッセージを使用します。

cary << Get Values;

{{"Cary", "Green Hope", "Panther Creek"}, 116244, "NC", "sunny"}

特定のキーの値だけを取得するには、キーを引数として指定します。その際、キーはリストで指定する必要があります。

cary << Get Values({"state", "population"});

{"NC", 116244}

1つのキーの値を取得するには、<<Get Valueメッセージを使用します。指定できるキーは1つだけで、リストを指定することはできません。

cary << Get Value("weather");

"sunny"

連想配列に含まれるすべてのキーと値のペアのリストを取得するには、<< Get Contentsメッセージを使用します。

cary << Get Contents;

{{"high schools", {"Cary", "Green Hope", "Panther Creek"}},

{"population", 116244},

{"state", "NC"},

{"weather", "sunny"}}

メモ: <<Get Contentsメッセージを使用して取得したリストには、デフォルト値は含まれません。キーはアルファベット順(文字コード順)で表示されます。

連想配列内の反復

連想配列の中を反復させるには、<<Firstメッセージと<<Nextメッセージを使用します。<<Firstは、連想配列の中の最初のキーを戻します。<<Next(key)は、引数として指定されたキー(key)の後のキーを戻します。

次のコマンドは、連想配列caryからキーと値のペアをすべて削除し、空の連想配列を残します。

currentkey = cary << First;
total = N Items( cary );
For( i = 1, i <= total, i++,
	nextkey = cary << Next( currentkey );
	Remove From( cary, currentkey );
	currentkey = nextkey;
);
Show( cary );

cary = [=>];

より詳細な情報が必要な場合や、質問があるときは、JMPユーザーコミュニティで答えを見つけましょう (community.jmp.com).