CompareArrays()

Summary

Compares two arrays, element by element.

Changes

Added to Version 2014.

Syntax

ret_array = CompareArrays (array arr1, array arr2, array options)

Argument Contents
Arr1 The first of the two arrays to compare
Arr2 The second of the two arrays to compare
Option Type Contents
Compare Macro String Name of a macro that will be called to compare elements in the array when comparing
Return Array Boolean If "True", the ArrayOfMatches array will be returned; the default is "False"
Return Booleans Boolean If "True", the return values in the returned ArrayOfMatches will be 1 for True and 0 for False as the result of the element by element comparison; the default is "False"

Returns

An options array with the following possible options:

Element Type Contents
Match Boolean 1, if all the values in the arrays are the same, otherwise 0
ArrayOfMatches Array Array of values with the results of the element by element comparison; only returned when the "Return Array" input option is "True".

Notes

  • The items in the arrays are compared in order. If the arrays need to be sorted before comparing them, they must first be sorted using SortArray().

  • The items in the arrays are compared using the same method as for SortArray(). If the "Return Array" option is specified without the "Return Booleans" option, each element in the returned array will be -1, 0 or 1.

  • CompareArrays() compares arrays with numeric or string contents.

  • If an array element is itself an array, the sort is performed using the value of the first element in the array.

  • If the arrays are of different lengths, the result returned in the "Match" option will always be "False"; if the "Return Array" option is specified, the length of the "ArrayOfMatches" returned array will be the length of the longer input array. The return values will depend on the lengths of the input arrays. For the length of the shorter array, the return values will be the same as for those returned when the input arrays are of the same length. For the remaining values, if the "Return Booleans" option is not specified, the values will be 1; if the "Return Booleans" option is specified, the values will be 0 for False.

Examples

Arr1 = {1, 2, 3, 4, 5}
Arr2 = {1, 2, 4, 4, 5}
Ret = CompareArrays(Arr1, Arr2, {{"Return Array", "True"}})
// Ret.Match = "False"
// Ret.ArrayOfMatches = {0, 0, -1, 0, 0}
Ret = CompareArrays(Arr1, Arr2, {{"Return Array", "True"}, {"Return Booleans", "True"})
// Ret.Match = "False"
// Ret.ArrayOfMatches = {1, 1, 0, 1, 1}
Arr1 = {1, 2, 3, 4, 5}
Arr2 = {1, 2, 3, 4, 5, 6} Ret = CompareArrays(Arr1, Arr2,
{{"Return Array", "True"}})
// Ret.Match = "False"
// Ret.ArrayOfMatches = {0, 0, 0, 0, 0, 1}

Error Codes

Error Type Meaning
Error Either Arr1 or Arr2 is null

See Also

Function Summary
SortArray() Sorts the elements of an array