Thanks:  0
Likes:  0
Dislikes:  0

# Thread: Compare Two Dimentional Array

1. ## Compare Two Dimentional Array

How can I compare 2 two diemantional Arrays and and get the matching values in one Array , and the Not matching values in other Array.

2. ## Re: Compare Two Dimentional Array

Hi,

See this thread. We need information I asked there to create an example.

3. ## Re: Compare Two Dimentional Array

There probably a better way of doing this, but this is what I came up with.

<font class="small">Code:</font><hr /><pre>
function TestArea()
{
var arr1 = new Array();
var arr2 = new Array();
var arr3 = new Array();

arr1 = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"];
arr2 = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
arr3 = ["a", "b", "c", "D", "E", "F", "A", "B", "d", "e"];

var data = Array_GetMatches(arr1, arr2); // returns 0 matches - 0 count
var data1 = Array_GetMatches(arr1, arr1); // returns all items - 10 count
var data2 = Array_GetNotMatches(arr1, arr2); // returns all items - 10 count
var data3 = Array_GetMatches(arr1, arr3); // returns "D", "E", "F" - 3 count
var data4 = Array_GetNotMatches(arr1, arr3); // returns all but "D", "E", "F" - 7 count

Log.Message("Data:" + data.length); // 0
Log.Message("Data1:" + data1.length); // 10
Log.Message("Data2:" + data2.length); // 10
Log.Message("Data3:" + data3.length); // 3
Log.Message("Data4:" + data4.length); // 7
}

function Array_GetMatches(arr1, arr2)
{
var ret = new Array();
var index = 0;
if(arr1.length &gt; arr2.length)
{
for(var i = 0; i &lt; arr1.length; i++)
{
if(i &lt; arr2.length)
{
if(arr1[i] == arr2[i]) ret[index++] = arr1[i];
}
}
}
else
{
for(var i = 0; i &lt; arr2.length; i++)
{
if(i &lt; arr1.length)
{
if(arr1[i] == arr2[i]) ret[index++] = arr1[i];
}
}
}
return ret;
}

function Array_GetNotMatches(arr1, arr2)
{
var ret = new Array();
var index = 0;
if(arr1.length &gt; arr2.length)
{
for(var i = 0; i &lt; arr1.length; i++)
{
if(i &lt; arr2.length)
{
if(arr1[i] != arr2[i]) ret[index++] = arr1[i];
}
else
{
ret[index++] = arr1[i];
}
}
}
else
{
for(var i = 0; i &lt; arr2.length; i++)
{
if(i &lt; arr1.length)
{
if(arr1[i] != arr2[i]) ret[index++] = arr2[i];
}
else
{
ret[index++] = arr2[i];
}
}
}
return ret;
}
</pre><hr />

These tests are linear, so it won't determine if a single item exists anywhere in the comparison array. Not to hard to implement, just have to loop through comparison array for each item in original array.

----------------
Aaron Foley

4. ## Re: Compare Two Dimentional Array

Just realized you asked for a 2D array comparison. The above are for single dimension, but can be modified.

----------
Aaron Foley

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
Search Engine Optimisation provided by DragonByte SEO v2.0.36 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
vBulletin Optimisation provided by vB Optimise v2.6.4 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
BetaSoft Inc.
Digital Point modules: Sphinx-based search
All times are GMT -8. The time now is 05:49 AM.