Creating 'Union', 'Intersection', 'Difference' and 'Complement' Arrays |
|
Method | Description | Logical Equivalent |
Contains all data from both arrays, eliminating any duplicates | A OR B | |
Contains only the data which is common to both arrays" | A AND B | |
Contains only the data which is not common to either array (basically the opposite of Intersection) | A OR B AND NOT(A AND B) |
|
Contains only data that is in one array, but not in the other array | A AND NOT(A AND B) |
For the purposes of this tip, we are going to work with two arrays, FruitA and ColorsA. (Why do both names end with "A"? Well two reasons: (1) it is a style convention I'm used to where all array variables end in "A" so I can recognize them when scanning a script, and (2) "Colors" is a keyword in Revolution, so you can't use it as an array name.)
Assume that the array FruitA contains the following elements (keys
):
(Note: Since we only care about the keys of an array, not the contents of each element, it doesn't matter what you put into each array element, so I'm using empty.)put "" into FruitA["apple"] put "" into FruitA["orange"] put "" into FruitA["pear"] put "" into ColorsA["red"] put "" into ColorsA["orange"] put "" into ColorsA["green"]
Union |
union
command in order to get the union of two arrays, with the syntax:
Keep in mind that this changes Array1 to now contain the union of both arrays, so if you need to keep Array1 unchanged, make sure you copy Array1 into another variable before you do theunion Array1 with Array2
union
. Here's how I would do
that with our sample arrays:
Which puts the following into the message box:put FruitA into FruitUnionColorsA union FruitUnionColorsA with ColorsA -- changes FruitUnionColorsA to be the union of FruitA and ColorsA put the keys of FruitUnionColorsA
apple orange pear red green
Intersection |
union
, Revolution provides the intersect
command in order to get the intersection of
two arrays, with the syntax:
Also likeintersect Array1 with Array2
union
Array1 will be changed to contain the intersection of both arrays. Here's how to use
intersect
, maintaining FruitA for use later:
Which puts the following into the message box:put FruitA into FruitIntersectColorsA intersect FruitIntersectColorsA with ColorsA -- changes FruitIntersectColorsA to be the intersection of FruitA and ColorsA put the keys of FruitIntersectColorsA
orange
Difference |
union
and returning the combined array as the returned value.
And here's how we'd use it:function difference pArray1,pArray2 put pArray1 into tempA intersect tempA with pArray2 repeat for each line tKey in (the keys of tempA) delete local pArray1[tKey] delete local pArray2[tKey] end repeat union pArray1 with pArray2 return pArray1 end difference
Which puts the following into the message box:put difference(FruitA,ColorsA) into FruitDiffColorsA put the keys of FruitDiffColorsA
pear green apple red
Complement |
And here's how we'd use it:function complement pArray1,pArray2 -- the first param is the array we want back; the second we don't care about put pArray1 into tempA intersect tempA with pArray2 repeat for each line tKey in (the keys of tempA) delete local pArray1[tKey] end repeat return pArray1 end complement
Which puts the following into the message box:put complement(FruitA,ColorsA) into FruitWithoutColorsA put the keys of FruitWithoutColorsA
pear apple
Well, there you have it... have fun working with arrays!
Posted 2/7/06 by Ken Ray