Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Check 2 arrays for difference

New Here ,
Apr 28, 2014 Apr 28, 2014

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!

TOPICS
ActionScript
1.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Apr 29, 2014 Apr 29, 2014

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 i

...
Translate
LEGEND ,
Apr 28, 2014 Apr 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 28, 2014 Apr 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 28, 2014 Apr 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 28, 2014 Apr 28, 2014

edit: Ack, 1 minute apart eheh.. Watch out, the OP is checking a property of a MovieClip which we know is dynamic. If the job number is "11" in one and "111" in the other, that will match. You should check for exactness, at least with == if not ===. I just used !=.

All you really need to do is loop one of the Arrays checking the property (or existence) for differences.. You're going in reverse which I assume you have a reason for so this function will return any differences from the Saved to the New array (switch them if you want it the other way around):

function checkJobArray():Array

{

          var differences:Array = [];

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

          {

                    // check if there's a difference

                    // verify value even exists

                    if (NewJobArray == undefined || NewJobArray.JobNumber != SavedJobArray.JobNumber)

                    {

                              // doesn't exist or is different

                              differences.push(NewJobArray.JobNumber);

                    }

                    else

                    {

                              // same

                              SameJobCount++;

                    }

          }

          return differences;

}

I just incremented your global SameJobCount if there was no difference in that index..

To use, just:

var differences:Array = checkJobArray();

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

trace(differences); // see the differences

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 29, 2014 Apr 29, 2014

Hello,

Thank you for your response, I really appreciate your help.

But I am still getting some issues...

when I run this code, it does not pick up the job number 444 (sometimes the savejobarray will have a smaller amount of jobs than newjobarray so it must be able to detect any extra jobs and return this value.

var SavedJobArray:Array = new Array();

var NewJobArray:Array = new Array();

var SameJobCount:int = 0;

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);

}

function checkJobArray():Array

{

          var differences:Array = [];

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

          {

                    // check if there's a difference

                    // verify value even exists

                    if (NewJobArray == undefined || NewJobArray.JobNumber != SavedJobArray.JobNumber)

                    {

                              // doesn't exist or is different

                              differences.push(NewJobArray.JobNumber);

 

                              trace("new job found");

                    }

                    else

                    {

                              // same

                              SameJobCount++;

                    }

          }

          return differences;

}

FillSavedArray();

FillNewArray();

var differences:Array = checkJobArray();

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

trace(differences); // see the differences

*when I run this code, this is what the output panel displays:

new job found

1 difference(s)

111

Thanks in advance!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 29, 2014 Apr 29, 2014

That's what I meant by "(switch them if you want it the other way around)", I don't know if one will always be bigger than the other or if either can be bigger. If either can be bigger you can detect it and use temp arrays (rather than branched code) similar to how you were using temp MovieClips.

Please take this fairly psuedo, I'm not checking exact spelling, I'm sure you will though.

e.g.

function checkJobArray():Array

{

          // differences in either

          var differences:Array = [];

          // larger (outer loop)

          var tempArr1:Array = SavedJobArray.length > NewJobArray.length ? SavedJobArray : NewJobArray;

          // smaller (inner loop)

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

          // iterate on larger array

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

          {

                    // check if there's a difference

                    // verify value even exists

                    if (tempArr2 == undefined || tempArr2.JobNumber != tempArr1.JobNumber)

                    {

                              // doesn't exist or is different

                              differences.push(tempArr2.JobNumber);

                              // optional - should you save also the value that differs in the first array?

                              // then uncomment this:

                             //differences.push(tempArr1.JobNumber);

                    }

                    else

                    {

                              // same

                              SameJobCount++;

                    }

          }

          return differences;

}

Note: This is working in a specialized way. Iterating over arrays in reverse, looking for any difference between them. It's only saving the JobNumber from the smaller array. If you also want the difference at the position in the other array, uncomment out the code I noted.

There's a lot of ways to do this type of thing. Not knowing your exact needs will only present one potential version of it. For example if reverse order wasn't important I'd just forward-iterate through each array marking differences and once either length was exhausted the entire rest of the longer array would be spliced as differences. You can do that in reverse too but I imagine these arrays aren't long enough to really require the best implementation anyhow.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 29, 2014 Apr 29, 2014

Ok let me clear up a few things.

The array does not need to be in reverse,

I made this sample code to get me started on the larger project which will contain 50-200 or more items in the array.

I want to put movie clips in the array because each job will have more than 1 info item (ex: JobNumber, JobTitle, JobState, etc) so this way I can put more items information in each MovieClip and then later have them on the screen for the user to interact with each job.

I am not an expert in AS3 so is there a way to do this in a more simplified way of doing this?

one other note: the saved job array once exhausted will then tell us that any other elements in the New Job array will contain new Jobs that must be tracked.

id like to keep this code short and simple so I can wrap my head around it, thanks again!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 29, 2014 Apr 29, 2014

To determine if there's an easier way we'd need more info.

Forward searching is good. Do you need to use a MovieClip or are you just using this because you can append properties to it (JobNumber, JobTitle, etc)? If you're not actually using a MovieClip for any other reason then you should be using an array of Objects.

e.g. (trying to keep it simple:

var job:Object = {

     'number':1,

     'title':'Some Title',

     'state': 3

};

NewJobArray.push(job);

// or without even creating the temp object: ----------

NewJobArray.push( { 'number':1, 'title':'Some Title', 'state': 3 } );

You'd access the properties the same way while iterating the array, e.g. NewJobArray.number or NewJobArray.title, etc..

After you elaborate on that, is it correct in saying you're ultimately just comparing the two arrays looking for differences at the same index in the array and you want to know just the differences (via an array of either job numbers, indexes or job objects, etc?)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 29, 2014 Apr 29, 2014

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 29, 2014 Apr 29, 2014

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*.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 30, 2014 Apr 30, 2014

Thank you very for your kind help, this will assit me a great deal!

It is working as intended and I will be able to finish my larger project thanks to your assistance.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 30, 2014 Apr 30, 2014
LATEST

If you're all set please mark any helpful/correct so we can filter answered questions.

You're welcome and good luck!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines