I need a sorting algorithm - comparing 2 arrays

  • clem_c_rock
  • Novice
  • Novice
  • clem_c_rock
  • Posts: 20

Post 3+ Months Ago

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: [ Select ]
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( _root.swf_names_array[i] == _root.txt_names_array[j] )
            {
                 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
                
            }
     }
}
  1. for( var i:Number = 0; i < _root.swf_names_array.length; i++ )
  2. {
  3.     for( var j:Number = 0; j < _root.txt_names_array.length; j++ )
  4.      {
  5.             if( _root.swf_names_array[i] == _root.txt_names_array[j] )
  6.             {
  7.                  trace( "Matches" );
  8.                  break;
  9.             }
  10.             else
  11.             {
  12.                  trace( " No match here" );
  13.                  not_found_array[j] = test_swf_name[0];  // add all non matches to the not found array
  14.                 
  15.             }
  16.      }
  17. }


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

Thanks for any time!
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • clem_c_rock
  • Novice
  • Novice
  • clem_c_rock
  • Posts: 20

Post 3+ Months Ago

Well - I think I have something that works although it's rather lengthy and not very gracefull.

Here it is:

Code: [ Select ]
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--; }
             }
        }
      //-------------------------------------------------
            
    }
    //-------------------------------------------------------
    
  1. var alert_message:String = '';
  2. var matches_found:Boolean = false;
  3. var not_found_array = Array();
  4. var not_found_cnt:Number = 0;
  5. var this_loop_not_found_cnt:Number = 0;
  6.     
  7. for( var i:Number = 0; i < _root.swf_names_array.length; i++ )
  8. {
  9.         matches_found = false;
  10.         this_loop_not_found_cnt = 0;
  11.         
  12.         if( _root.txt_names_array.length == 0 ){ not_found_array[0] = _root.swf_names_array[i]; } //if not one text file
  13.         
  14.         for( var j:Number = 0; j < _root.txt_names_array.length; j++ )
  15.       {
  16.              if( _root.swf_names_array[i] == _root.txt_names_array[j] )
  17.              {
  18.                    matches_found = true; trace( "*****MATCH FOUND" );
  19.           break;
  20.              }
  21.              else
  22.              {                
  23.                   not_found_array[not_found_cnt] = _root.swf_names_array[i];
  24.                   not_found_cnt++;
  25.                   this_loop_not_found_cnt++;
  26.              }
  27.             
  28.         }//___endFor2____
  29.         
  30.       //---- clean out the not found array if we have a match
  31.         //-------------------------------------------------
  32.         if( matches_found == true )
  33.         {
  34.              for( var k:Number = not_found_array.length ; k >= 0; k-- )
  35.              {
  36.                   if( not_found_array[k] != undefined ){ not_found_array.splice(k, 1); not_found_cnt--; }
  37.              }
  38.         }
  39.       //-------------------------------------------------
  40.             
  41.     }
  42.     //-------------------------------------------------------
  43.     



Does anyone see how I could streamline this code?

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 7 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.