Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 09-26-2007, 11:05 AM   #1
ljonny18
Registered User
 
Join Date: Oct 2006
Posts: 112
Default comparing arrays

Hi,

I have 2 arrays that I want to compare:

var a1:Array = new Array(2,1,3,0);
var a2:Array = new Array(0,2,3);

I want to compare the values in the arrays, and all the values that exist in a1 and dont exist in a2 (in this case 1 and 4), I want to put into a new array (a3):

a3 = (1,4)

how can I do this in AS3???

Thanks,
Jon.
ljonny18 is offline   Reply With Quote
Old 09-27-2007, 01:04 PM   #2
flexy
Everything about me's...
 
Join Date: Aug 2006
Location: UK
Posts: 198
Default

I think you'd need to iterate through your arrays:

Code:
private function matchArrays( a1:Array, a2:Array ) :Array
{
  var a3:Array = new Array();
  var len1:int = a1.length;
  for( var i:int = 0; i<len1; i++ )
  {
      var len2:int = a2.length;
      for( var j:int = 0; j<len2; j++ )
      {
            if( a1[i] == a2[j] )
            {
                a3.push( a1[i] );
                break;
            }
      }
  }
  return a3;
}
flexy is offline   Reply With Quote
Old 09-27-2007, 03:25 PM   #3
ljonny18
Registered User
 
Join Date: Oct 2006
Posts: 112
Default

that is what I tried and was having problems with

in your example a3 = 2,3,0, and I only need to return the values that are in the first array and not in the second array etc....

e.g.

a1:Array = new Array(2,1,3,0);
a2:Array = new Array(0,2,3);

so I need a2:Array = new Array(1); - as "1" is tha value that is in the first array but not in the second.

Thanks,
Jon.
ljonny18 is offline   Reply With Quote
Old 09-27-2007, 03:37 PM   #4
flexy
Everything about me's...
 
Join Date: Aug 2006
Location: UK
Posts: 198
Default

How about?

Code:
private function matchArrays( a1:Array, a2:Array ) :Array
{
  var a3:Array = new Array();
  var len1:int = a1.length;
  var notFound:Boolean = false;
  for( var i:int = 0; i<len1; i++ )
  {
      if( notFound )
      {
           a3.push( a1[i-1] )
           notFound = false;
      }

      var len2:int = a2.length;
      for( var j:int = 0; j<len2; j++ )
      {
            if( a1[i] == a2[j] )
            {
                notFound = false;
                break;
            }
            else
            {
                notFound = true;
            }
      }
  }
  return a3;
}
flexy is offline   Reply With Quote
Old 09-28-2007, 01:35 AM   #5
Jim Freer
Senior Member
 
Join Date: Jul 2007
Location: Houston, TX
Posts: 355
Send a message via Yahoo to Jim Freer
Default

ActionScript Code:
private function aNotInB     ( avArrayA :Array,       avArrayB :Array )     :Array {     var lvArray:Array = [];         for each( var avObjectA:Object in avArrayA )     {         if( avArrayB.indexOf( avObjectA ) == - 1 )             lvArray.push( avObjectA );     } // for each         return lvArray;     } // aNotInB

http://freerpad.blogspot.com/
Jim Freer is offline   Reply With Quote
Old 09-28-2007, 09:57 AM   #6
ljonny18
Registered User
 
Join Date: Oct 2006
Posts: 112
Default

thats great - thanks to both of you.

I used something similar to "Jim Freer's" example in the end

cheers guys,

Jon.
ljonny18 is offline   Reply With Quote
Old 09-28-2007, 10:53 AM   #7
flexy
Everything about me's...
 
Join Date: Aug 2006
Location: UK
Posts: 198
Default

Yeh Jim's for...each is a better way of doing it.
flexy is offline   Reply With Quote
Old 02-27-2009, 08:45 AM   #8
likhim
Registered User
 
Join Date: Feb 2009
Posts: 1
Default Compare ArrayCollection

May I know how to compare two arraycollection and return the result as arraycollection?
likhim is offline   Reply With Quote
Old 05-20-2009, 11:18 PM   #9
randygland
Registered User
 
Join Date: Nov 2008
Posts: 3
Default comparing arrays

it's nice to see the logic of the longer version though, I find it easier to follow, even if it's less clean.
likhim, I need to do the same.. and will probably end up doing it by stripping relevant data from the ArrayCollections I want to compare into new Arrays.. comparing those arrays with Jim's for each function, then reconstructing the ARrayCollections with the results.

I will post the code.
randygland is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparing Arrays help zoomfreddy ActionScript 1.0 (and below) 5 06-24-2010 02:05 PM
Comparing Two Arrays rectangle ActionScript 2.0 6 07-25-2007 09:42 PM
Comparing and Acting on Differences in Arrays timothygray ActionScript 2.0 1 08-01-2006 04:14 PM
Comparing arrays Dave At ADC ActionScript 2.0 5 11-30-2005 10:59 PM
Comparing the elements of 2 arrays A4u ActionScript 1.0 (and below) 7 08-29-2003 01:56 AM


All times are GMT. The time now is 02:55 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.