# Question about arrays and logical operators

Hi, everyone. I’m working on some script that requires testing whether one array is equal to another. Here is the general gist of the code:

myArray = new Array(0,0,1);
yourArray = new Array(0,0,1);

if(myArray == yourArray){
trace(“condition true”);
}
else{
trace(“condition false”);
}

Although this script seems like it should work, the test always returns ‘false’. Any thoughts on how I can determine whether two arrays are identical? (I realize that I can break it down and test element by element [ if( (myArray[0]==yourArray[0]) && (myArray[1]==yourArray[1]) && (myArray[2]==yourArray[2]))], but there must be an easier way!)

Thanks in advance.

Chris

array variable names are ‘references’ or ‘pointers’ to the actual array in memory, like a program shortcut in windows or a mac alias. This means that the actual value of myArray or yourArray is that actual point in memory. The only time you can compare two array variable names with each other and have them equal is when you specifically assigned one to equal the other. In this case, unlike with numbers, your array isnt copied, but rather the reference to the array in memory is copied so the variables reference the same array. ex:

a = [1,2,3,4];
b = a;

trace(a) // 1,2,3,4
trace(b) // 1,2,3,4

trace(a == b) // true

b was assigned to reference the same array as a was with b = a; So when compared, they are equivalent. Now looking at this:

a = [1,2,3,4]
b = [1,2,3,4]

trace(a) // 1,2,3,4
trace(b) // 1,2,3,4

trace(a == b) // false

here when a was created it referenced the point in memory where those specific numbers 1,2,3 and 4 were kept. When b was created, a new set of numbers 1,2,3 and 4 were created somewhere in memory and b was assigned to that. So when compared in that final trace, those two points in memory are not the same hense the result given is false

now before I get into how to check to see if they are equivalent, let me get into the troubles with the first code example and using something like b = a; Since we now know (if you didnt already) that array variable names are references, we can assume that when one reference is set to equal another then the reference itself is copied. And this means ONLY the reference and whatever it is the variables are referencing. For instance (windows ex:) if you have a shortcut of flash on your desktop and you copy it into my Documents, the actual Flash.exe isnt copied too. Its still sitting pretty where it always was in the program files directory. Only the shortcut reference itself was copied despite that both shortcuts still point to the same exe. So, given

a = [1,2,3,4];
b = a;

if we then say something like

b[0] = “sandwich”;

then that change is reflected in the actual array itself. And because both b AND a point to that exact same array, that means the ‘a’ array is also changed

trace(a); // sandwich,2,3,4

So this is something to be careful of. If you want to copy an array and not just make another reference to one use

b = a.slice()

which uses slice to return a copy of the array a and assign that to the variable name b.

Now, getting past all that, to compare two arrays with eachother, you will essentially have to go through and compare each value one by one with each other and make sure they all match. Seems tedious but with the help of a prototype function and a while loop, it will be no problem at all. Not to delay any further, lets just jump right in and spit out some code:

``````
Array.prototype.equals = function(a2){
var i = this.length;
if (i != a2.length) return false;
while(i--) if (this[i**] != a2[i**]) return false;
return true;
}

a = [1,2,3,4];
b = [1,2,3,4];
c = [1,2,3,5];

trace(a.equals(b)); // true
trace(a.equals(c)); // false

``````

So whats happening here is an array prototype is made to give all arrays this new function called ‘equals’ which takes one argument of ‘a2’ which represents array number 2 and compares each element of that array with the given array. If there is a case where the elements dont match, false is returned, otherwise true is returned. To help speed up the function theres an extra check at the top making sure that the lengths of the arrays match before cycling throuh all the values of the array since if the lengths dont match, the arrays wont be equal and theres no need to try to go through anc compare each element.

So once this equals function is defined in your movie (best placed in frame 1) it can be used at anytime anywhere just like array.pop() or array.slice(), giving you an easy way to compare arrays and check to see if they are ‘equal’.

Thanks, senocular! That was very helpful. I’ve added you to my list of people I revere.

glad I could help

:beam:

Nice. This is totally ratable :+)