Skip to main content
Known Participant
April 28, 2014
Answered

Check 2 arrays for difference

  • April 28, 2014
  • 1 reply
  • 1547 views

Hello,

I need some help, I am trying to loop through two arrays that contain movie clips, and compareing them for differences

here is my code so far:

var SavedJobArray:Array = new Array();

var NewJobArray:Array = new Array();

function FillSavedArray():void

{

          var Temp:MovieClip = new BlackMC();

          Temp.JobNumber = 11;

          SavedJobArray.push(Temp);

          var Temp2:MovieClip = new BlackMC();

          Temp2.JobNumber = 22;

          SavedJobArray.push(Temp2);

          var Temp3:MovieClip = new BlackMC();

          Temp3.JobNumber = 33;

          SavedJobArray.push(Temp3);

}

function FillNewArray():void

{

          var Temp:MovieClip = new BlackMC();

          Temp.JobNumber = 111;

          NewJobArray.push(Temp);

          var Temp2:MovieClip = new BlackMC();

          Temp2.JobNumber = 22;

          NewJobArray.push(Temp2);

          var Temp3:MovieClip = new BlackMC();

          Temp3.JobNumber = 33;

          NewJobArray.push(Temp3);

 

          var Temp4:MovieClip = new BlackMC();

          Temp4.JobNumber = 444;

          NewJobArray.push(Temp4);

}

var SameJobCount:int = 0;

function checkJobArray():void

{

          trace("saved Job Array L:"+SavedJobArray.length);

          trace("new Job Array L:"+NewJobArray.length);

          var TempSaved:MovieClip;

          for (var i:int = SavedJobArray.length-1; i>=0; i--)

          {

                    TempSaved = SavedJobArray;

                    var TempNew:MovieClip;

                    for (var j:int = NewJobArray.length-1; j>=0; j--)

                    {

                              TempNew = NewJobArray;

                              if (TempSaved.JobNumber == TempNew.JobNumber)

                              {

                                        SameJobCount++;

                                        trace("match: "+TempSaved.JobNumber+" & "+TempNew.JobNumber);

                                        //if match found, remove them from both arrays

                                        SavedJobArray.splice(SavedJobArray,1);

                                        NewJobArray.splice(NewJobArray,1);

                              }

                    }

          }

          traceNewJobs();

}

function traceNewJobs():void

{

          var TempNew:MovieClip;

                    for (var j:int = NewJobArray.length-1; j>=0; j--)

                    {

                              TempNew = NewJobArray;

 

                              trace("NEW JOB: "+TempNew.JobNumber);

                    }

}

FillSavedArray();

FillNewArray();

checkJobArray();

what I wanna be able to do, is loop through both arrays and see if the "job numbers don't match"... compare the savedjobarray with the newjobarray and see if the new job array contains any job numbers that the Saved Job array does not contain.

I tried something different and any that do match I removed them from the arrays.. but its not really working out.

I hope this made sense,

thanks in advance for your help!

This topic has been closed for replies.
Correct answer sinious

Yes i need to use movielip because they are going to be clickable items on the screen. I will also being moving the objects around.

when I add data to the array per movieclip it is done by downloading an XML (here is some code)

TotalJobs = (myXML.*.length())/6;

 

          for (var p:int=0; p<TotalJobs; p++)

          {

                    var Temp:MovieClip = new BlackMC();

                    Temp.JobNumber = myXML.JobNumber

;

                    Temp.JobTitle = myXML.JobTitle

;

                    Temp.JobCity = myXML.JobCity

;

                    Temp.JobState = myXML.JobState

;

                    Temp.JobZipCode = myXML.JobZipCode

;

                    Temp.JobType = myXML.JobType

;

 

                    JobArray.push(Temp);

          }

to clear up your second question, I am comparing two arrays but I do not know if the objects will be at the same index so this is not a good way to do a compare.

What my App will do, is have the user download this XML in the foreground and then 3 hours later in the background (when user is not using the app) and check if any new jobs have been downloaded... since the JobNumber is the only property that can not be duplicated, this the the property I want to check if there are any new jobs downloaded.

so I just want to loop through this savedjobarray and the newjobarray and see if any new jobs are in the new jobarray. and count how many, then put these "NEW jobs" in a new array.

Thanks


Ok so all that matters is the job numbers and the items can appear anywhere in either list. You just want the unique job numbers in either list. To do that I'd iterate through both arrays gathering the job numbers in two temp arrays. After that I'd iterate through the longest temp array, eliminating any job numbers (from both temp arrays) that match. What you will be left with is potentially two arrays of non-matching job numbers you can combine into a single list.

Sounds more complicated than it is.

function checkJobArray():Array

{

          // larger (outer loop)

          var tempArr1:Array;

          var arrayOrder:int = 1; // 1=Saved is temp1, 2=New is temp1

          if (SavedJobArray.length > NewJobArray.length)

          {

                     tempArr1 = SavedJobArray

          }

          else

          {

                    tempArr1 = NewJobArray;

                    // since New is tempArr1, adjust arrayOrder

                    // which is used later to return results

                    arrayOrder = 2;

          }

          // smaller (inner loop)

          var tempArr2:Array = SavedJobArray.length < NewJobArray.length ? SavedJobArray:NewJobArray;

          // iterate on larger array

          for (var i:int=0; i < tempArr1.length; i++)

          {

                    // check if this job matches the smaller array (iteration)

                    for (var j:int=0; j < tempArr2.length; j++)

                    {

                              // match?

                              if (tempArr1.JobNumber == tempArr2.JobNumber)

                              {

                                        // yes, remove from both

                                        tempArr1.splice(i,1);

                                        tempArr2.splice(j,1);

                                        // we reduced the array at this index,

                                        // must reduce the index to properly keep going

                                        i--;

                                        j--;

                                        // increase matched jobs

                                        SameJobCount++;

                              }

                    }

          }

          // all that is left are jobs that don't match,

          // want a single list? concat them together:

          return tempArr2.length > 0 ? tempArr1.concat(tempArr2) : tempArr1;

          // want separate lists? return separate arrays [SavedJobs, NewJobs]

          // utilizing arrayOrder from above to determine the correct order.

          // return arrayOrder == 1 ? [tempArr1, tempArr2] : [tempArr2, tempArr1];

}

Testing:

var differences:Array = checkJobArray();

trace(differences.length + ' difference(s)');

for (var idx:int = 0; idx < differences.length; idx++)

{

          trace(differences[idx].JobNumber); // see the differences

}

Traces:

3 difference(s)

111

444

11

As with anything, this has a limit. If you have any repeated JobNumbers then only 1 matched instance of them will be removed, leaving behind the duplicate JobNumber as a difference.

e.g. psuedo example (meaning it's not code you can use, just understand)

var NewNumbers:Array = [ 1, 1, 2, 3 ];

var SavedNumbers:Array  = [ 1, 2, 3, 4 ];

If those arrays could run through this function, only the first duplicated '1' would be removed from both arrays, leaving another '1' in NewNumbers. By the end the difference would be: 1, 4

You may want that *shrug*.

1 reply

Ned Murphy
Legend
April 28, 2014

If you want to see if the newjobarray contains anything that is not in the savedjobarray then you could just use the indexOf method and loop thru the newjobarray making the test...

     if(savedjobarray.indexOf(newjobarray == -1)   ... i being the loop index

... that test being true indicates there is an element in newjobarray at index i that is not in the savedjobarray

v4varun22Author
Known Participant
April 28, 2014

Ned,

Thank you for your response.

But I just want to make sure I got this right, here is what I gather from your statement:

so my check function should look like this?

function checkJobArray():void

{

          var TempSaved:MovieClip;

          for (var i:int = SavedJobArray.length-1; i>=0; i--)

          {

                    TempSaved = SavedJobArray;

                    var TempNew:MovieClip;

                    for (var j:int = NewJobArray.length-1; j>=0; j--)

                    {

                              TempNew = NewJobArray;

 

                              if(SavedJobarray.indexOf(NewJobArray == -1)

                                 {

                                           //found a new job

                                 }

                    }

          }

}

So how come there is no check for the JobNumber?

Thanks.

Ned Murphy
Legend
April 28, 2014

You do not need the outer loop, just the inner one.  That way you check the entire savedjob array against each element of the newjobarray.

function checkJobArray():void

{

                    for (var i:int = NewJobArray.length-1; i>=0; i--)

                    { 

                              if(SavedJobarray.indexOf(NewJobArray) == -1)

                                 {

                                           //found a new job

                                 }

                    }

}

(note I added a closing parenthesis I missed earlier... also, I removed the extra code you had for lack of knowing what it's intended to do, not because it is wrong or whatever.