User Tools

Site Tools


comparing_20arrays_20for_20equality

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
comparing_20arrays_20for_20equality [2018/03/31 13:19] – external edit 127.0.0.1comparing_20arrays_20for_20equality [2024/01/05 00:22] (current) – external edit 127.0.0.1
Line 2: Line 2:
  
 //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:\\  //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:\\ 
 +<code bb4w>
         array1() -= array2()         array1() -= array2()
         IF MOD(array1()) = 0 THEN         IF MOD(array1()) = 0 THEN
Line 8: Line 9:
           PRINT "Arrays are different"           PRINT "Arrays are different"
         ENDIF         ENDIF
 +</code>
 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():\\  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():\\ 
 +<code bb4w>
         array1() -= array2()         array1() -= array2()
         equal% = MOD(array1()) = 0         equal% = MOD(array1()) = 0
         array1() += array2()         array1() += array2()
 +</code>
 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):\\  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):\\ 
 +<code bb4w>
         test() = array1() - array2()         test() = array1() - array2()
         equal% = MOD(test()) = 0         equal% = MOD(test()) = 0
 +</code>
comparing_20arrays_20for_20equality.1522502350.txt.gz · Last modified: 2024/01/05 00:18 (external edit)