Comparing Arrays

Recommended Posts

What's the way to compare two arrays? I am using an IF instruction and it only compares the first value of the array.

```private.test1 = {0,1,0,0}
private.test2 = {1,0,1,0}

if(private.test1 == private.test2)
? "Equal"
else
? "Not Equal"
endif```

This code gives me 'Not Equal" as I expected.

But if I change to:

```private.test1 = {1,1,0,0}
private.test2 = {1,0,1,0}

if(private.test1 == private.test2)
? "Equal"
else
? "Not Equal"
endif```

I get "Equal" and I was surprised. After playing around a bit I found that the IF only checks the first value (i.e. private.test1[0]). Do I need to use channels instead of private variables?

Thanks.

Share on other sites

Actually, doing (and note that I don't use the old private. notation):

test1 == test2

returns an array of values. In your second example, it would return:

{1,0,0,1}

however, the if() statement only looks at the first element of this resultant array because it doesn't look at the expression, just the end result. The shortcut I use for what you are trying to do is by using Min or max(), so

```if (min(test1 == test2))
? "all equal"
endif```

min(test1 == test2) will only be 1 if all values are 1.

Ok that works.

Thank you.