PDA

View Full Version : Array intersection



bluejay47
July 14th, 2007, 08:45 AM
Hi everyone, I'm new to the forum and to actionscript. I was wondering if there is a way to check 2 different arrays for intersection?

bluejay47

FizixMan
July 14th, 2007, 09:05 AM
Could write your own function that runs through each element of the arrays and compares them.

akurtser
July 14th, 2007, 12:22 PM
Here is something I found on google:

http://ketox.org/jon/blog/codespace/2005/10/array-intersection.html



Hi everyone, I'm new to the forum and to actionscript. I was wondering if there is a way to check 2 different arrays for intersection?

bluejay47

bluejay47
July 16th, 2007, 02:48 PM
This what I have so far. I want to compare the user created "currentselections" array with an already declared array called "k45", but am not sure where the comparison would logically go within the script?

/*gets length of array, gets position of item*/
on (release) {
Array.prototype.getIndex = function(data) {
for (i=0; i<this.length; ++i) {
if (this[i] == data) {
return i;
}
}
return -1;
};

location = currentselections.getIndex("c");
trace(location);

/*if item no in array, add item to array*/
if (location == -1) {
i = currentselections.length;
currentselections[i]="c";
trace(currentselections);
}
/*if item in array, remove item from array*/
else {
currentselections.splice(location, 1);
trace(currentselections);
}

}

irrationalistic
July 16th, 2007, 03:01 PM
function getIntersect(arr1:Array, arr2:Array):Array {
var temp:Array = new Array();
for(var i = 0; i < arr1.length; i++){
for(var k = 0; k < arr2.length; k++){
if(arr1[i] == arr2[k]){
temp[temp.length] = arr1[i];
}
}
}
return temp;
}

This function will return an array made up entirely of the elements contained in both arrays passed to the function...

Maybe that will help a bit...