Trace:

comparing_20arrays_20for_20equality

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

comparing_20arrays_20for_20equality [2018/03/31 13:19] 127.0.0.1 external edit |
comparing_20arrays_20for_20equality [2018/04/17 15:20] (current) tbest3112 Added syntax highlighting |
||
---|---|---|---|

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.txt ยท Last modified: 2018/04/17 15:20 by tbest3112

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International