Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

x I need a sorting algorithm - comparing 2 arrays

Status
Not open for further replies.

clemrock

Programmer
May 20, 2004
118
0
0
US
Hello,

I'm trying to compare 2 arrays and find all the unique elements of the 2nd array that doesn't match the elements of the first array.

I'm trying this but I keep coming up w/ data that matches that's actually supposed to show as a non-match (if that makes any sense).

Here's what I'm trying so far:

Code:
for( var i:Number = 0; i < _root.swf_names_array.length; i++ )
{
for( var j:Number = 0; j < _root.txt_names_array.length; j++ )
{
if( test_swf_name[0] == test_txt_name[0] )
{
trace( "Matches" );
break;
}
else
{
trace( " No match here" );
not_found_array[j] = test_swf_name[0]; // add all non matches to the not found array

}
}
}

I just don't know why this is hard - maybe I need more sleep.

Thanks for any time!
 
Well - I think I have something that works although it's rather lengthy and not very gracefull.

Here it is:

Code:
var alert_message:String  = '';
var matches_found:Boolean = false;
var not_found_array = Array();
var not_found_cnt:Number = 0;
var this_loop_not_found_cnt:Number = 0;
   
for( var i:Number = 0; i < _root.swf_names_array.length; i++ )
{
      matches_found = false;
      this_loop_not_found_cnt = 0;
      
      if( _root.txt_names_array.length == 0 ){ not_found_array[0] = _root.swf_names_array[i]; } //if not one text file
      
      for( var j:Number = 0; j < _root.txt_names_array.length; j++ )
      {
           if( _root.swf_names_array[i] == _root.txt_names_array[j] )
           {
                 matches_found = true;  trace( "*****MATCH FOUND" );
                   break;
           }
           else
           {            
                not_found_array[not_found_cnt] = _root.swf_names_array[i];
                not_found_cnt++;
                this_loop_not_found_cnt++;
           }
         
      }//___endFor2____
      
       //---- clean out the not found array if we have a match
      //-------------------------------------------------
      if( matches_found == true )
      {
           for( var k:Number =  not_found_array.length ; k >= 0; k-- )
           {
                if( not_found_array[k] != undefined ){ not_found_array.splice(k, 1);  not_found_cnt--;  }
           }
      }
      //-------------------------------------------------
         
   }
   //-------------------------------------------------------



Does anyone see how I could streamline this code?
 
[tt]// Return an Array of array2 elements not found in array1
function findUniqueElements(array1:Array, array2:Array):Array {
var arr:Array = new Array();
while (array2.length) {
var testElement:Object = array2.shift();
var match:Boolean = false;
for (var i = 0, n = array1.length; i<n; i++) {
if (array1 == testElement) {
match = true;
break;
}
}
if (!match) {
arr.push(testElement);
}
}
return arr;
}
//[/tt]

If you do:
[tt]//
var arrX:Array = ["A", "B", "C", "D"];
var arrY:Array = ["A", "X", "B", "C", "Y"];
trace(findUniqueElements(arrX, arrY));
//[/tt]

You'll get:
[tt]//Output
X,Y
//
[/tt]

Kenneth Kawamoto
 
Actually, shift() will destroy the original array. If you don't want that then this is the corrected version:
[tt]
// Return an Array of array2 elements not found in array1
function findUniqueElements(array1:Array, array2:Array):Array {
var arr:Array = new Array();
for (var j = 0, m = array2.length; j<m; j++) {
var testElement:Object = array2[j];
var match:Boolean = false;
for (var i = 0, n = array1.length; i<n; i++) {
if (array1 == testElement) {
match = true;
break;
}
}
if (!match) {
arr.push(testElement);
}
}
return arr;
}
//
[/tt]

Kenneth Kawamoto
 
Thanks a lot- very elegent.

Makes my mess look like a bloated pig!

Cheers!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top