For the latest version of JMP Help, visit JMP.com/help.


Publication date: 07/24/2024

Create Associative Arrays

To create an empty associative array using JSL, use the Associative Array() function or [=>].

cary = Associative Array();
cary = [=> ];

Associative array( 0 elements ) assigned.

Keys and values can be any JSL objects. Items can be added and changed with subscripting:

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"};

Default Values

A default value determines the value of a key that does not exist in an associative array. If you try to access a key that does not exist in an associative array, an error results. If you define a default value for your associative array, accessing a key that does not exist results in the following:

adds the key to the associative array

assigns the default value to the new key

returns the new key’s (default) value instead of an error

If you construct an associative array from a list of strings without assigning values to the keys, then the keys are assigned values of 1. The default value for the associative array is set to 0.

To set the default value:

cary = Associative Array();
cary << Set Default Value( "Cary, NC" );

To determine whether there is a default value set for an associative array, use the <<Get Default Value message.

cary << Get Default Value;

"Cary, NC"

If there is no default value, Empty() is returned.

Besides the Set Default Value message, a default value can be set in the literal constructor using =>value without a key.

counts = ["a" => 10,
"b" => 3,
=> 0]; // default value of 0
counts["c"] += 1;
Show( counts );

counts = ["a" => 10, "b" => 3, "c" => 1, => 0];

In the first line, the default value is set to 0. In the second line, the key "c" does not exist in counts. In the output, the key "c" is created with the default value of 0 and then incremented by 1.

Note: If a key’s value is the default value, then the key is dropped because any key will return the default value.

Associative Array Constructors

Create an empty associative array:

map = [=>];
map = Associative Array();

Create an empty associative array with a default value:

map = [=>0];
map = Associative Array( 0 );

Create an associative array with specific values:

map = ["yes" => 0, "no" => 1];

Create an associative array with specific values with a default value:

map = ["yes" => 0, "no" => 1, => 2];

Create an associative array with specific values that contains an associative array:

map = ["yes" => 0, "no" => 1, "alt" => ["a" => "1", "b" => "2"]];

Create an associative array from a list that contains two lists of a key-value pair:

map = Associative Array( {{"yes", 0}, {"no", 1}} );

Create an associative array from a list that contains two lists of a key-value pair with a default value:

map = Associative Array( {{"yes", 0}, {"no", 1}}, 2 );

Create an associative array from a list of keys and a list of values:

map = Associative Array( {"yes", "no"}, {0, 1} );

Create an associative array from a list of keys and a list of values with a default value:

map = Associative Array( {"yes", "no"}, {0, 1}, 2 );

Create an associative array from two column references. The first column contains the keys and the second contains the values.

map = Associative Array( :name, :height );

Create an associative array from two column references with a default value:

map = Associative Array(:name, :height, .);

Create an associative array from a single list of keys or a single column reference of keys with a default value of 0:

set = Associative Array( {"yes", "no"} );
set = Associative Array( :name );
Want more information? Have questions? Get answers in the JMP User Community (community.jmp.com).