Publication date: 07/30/2020

You can also use associative arrays to perform set operations. The following examples show how to take a union of two sets, a difference of two sets, and an intersection of two sets.

First, create three sets and view them in the log:

set_y = Associative Array( {"chair", "person", "relay", "snake", "tripod"} );

set_z = Associative Array( {"person", "snake"} );

set_w = Associative Array( {"apple", "orange"} );

// write the sets to the log

`Write(`

` "\!NExample:\!N\!tset_y = ",`

` set_y << Get Keys,`

` "\!N\!tset_z = ",`

` set_z << Get Keys,`

` "\!N\!tset_w = ",`

` set_w << Get Keys`

);

Example:

set_y = {"chair", "person", "relay", "snake", "tripod"}

set_z = {"person", "snake"}

set_w = {"apple", "orange"}

To find the union of two sets, insert one set into the other:

`set_z << Insert( set_w );`

Write( "\!N\!NUnion operation (set_w, set_z):,\!N\!tset_z = ", set_z << Get Keys );

Union operation (set_w, set_z):,

set_z = {"apple", "orange", "person", "snake"}

To find the difference of two sets, remove one set from the other:

`set_y << Remove( set_z );`

Write( "\!N\!NDifference operation (set_z from set_y):\!N\!tset_y = ", set_y << Get Keys );

Difference operation (set_z from set_y):

set_y = {"chair", "relay", "tripod"}

To find the intersection of two sets, use the aa << Intersect message.

`set_w << Intersect( set_z );`

Write( "\!N\!NIntersect operation (set_w, set_z):\!N\!tset_w = ", set_w << Get Keys );

Intersect operation (set_w, set_z):

set_w = {"apple", "orange"}

Given a list of names, which of them are not contained in Big Class.jmp? You can find the answer by taking the difference of the two sets of names.

1. Get the list of names and open the data table:

names list = {"ROBERT", "JEFF", "CHRIS", "HARRY"};

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );

2. Put the list of names into an associative array:

`names = Associative Array( names list );`

3. Perform a difference operation by removing the column values from your list:

names << Remove( Associative Array( dt:name ) );

4. Look at the result:

Write( "\!NWhich of {ROBERT, JEFF, CHRIS, HARRY}, is not in Big Class = ", names << Get Keys );

Which of {ROBERT, JEFF, CHRIS, HARRY}, is not in Big Class = {"HARRY", "JEFF"}

Want more information? Have questions? Get answers in the JMP User Community (community.jmp.com).