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
Comparing arrays
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
- VIP Livecode Opensource Backer
- Posts: 10057
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: Comparing arrays
If comparing keys only see the intersect command.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: Comparing arrays
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.
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.
shiftLock happens
Re: Comparing arrays
Thank you, that gives me some ideas.