User Tools

Site Tools


Comparing arrays for equality

by Richard Russell, November 2012

Supposing you have two numeric arrays (of the same size) and you want to test whether they are identical, i.e. that their contents are the same. Obviously you could iterate through the indices, comparing each element in turn with the corresponding element in the other array, but that's a bit messy. Luckily there is a much neater way to do it:

        array1() -= array2()
        IF MOD(array1()) = 0 THEN
          PRINT "Arrays are identical"
          PRINT "Arrays are different"

This works because the MOD(array()) function returns zero only if all the elements of the array are zero.

Of course the above test is destructive of the contents of array1(), which may not be desirable. If you want to preserve the contents of the array one method is to reverse the effect of the subtraction by adding back array2():

        array1() -= array2()
        equal% = MOD(array1()) = 0
        array1() += array2()

Here equal% is set TRUE if the arrays are identical and FALSE otherwise.

Another way of preserving the contents of array1() is to use a third array (with the same dimensions as the other arrays):

        test() = array1() - array2()
        equal% = MOD(test()) = 0
This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
comparing_20arrays_20for_20equality.txt · Last modified: 2018/04/17 16:20 by tbest3112