Page 1 of 1

Comparing arrays

Posted: Tue Oct 04, 2016 2:19 pm
by Jeffrey P
I'm trying to figure out the best way to compare two arrays.
My program needs to essentially do a "diff" on two arrays and report keys that are only found in Array A and only found in Array B, as well as values that are different between the two arrays.
Additionally,the arrays are nested with sub-arrays.

Can anybody point me at some sample code or some hints at the most efficient way of doing something like this? I thought about the possibility of converting the arrays to ordered text and then comparing lines like you would standard text, but maybe there is a different or better way.

Thanks in advance for any and all advice.

Jeffrey

Re: Comparing arrays

Posted: Tue Oct 04, 2016 5:02 pm
by FourthWorld
If comparing keys only see the intersect command.

Re: Comparing arrays

Posted: Tue Oct 04, 2016 11:48 pm
by [-hh]
Yet another option.
Start with a comparison of "simple" arrays:
Combine the arrays (as you intended, ordering is not needed). You may use the combine command for that.
Then you have ordered sets and can use the stack "countedSets" (#25) from here:
http://forums.livecode.com/viewtopic.ph ... 57#p101857

This gives you an info about the symmetric difference: Members of A without B, B without A, and their values. All in one job. There is help in the stack to the technique used (it's an array technique by itself).

For arrays of arrays you have to work harder but the technique remains the same.

Re: Comparing arrays

Posted: Wed Oct 05, 2016 5:29 pm
by Jeffrey P
Thank you, that gives me some ideas.