Comparing arrays

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Jeffrey P
Posts: 2
Joined: Mon Mar 11, 2013 11:38 pm

Comparing arrays

Post by Jeffrey P » Tue Oct 04, 2016 2:19 pm

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

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10057
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Comparing arrays

Post by FourthWorld » Tue Oct 04, 2016 5:02 pm

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

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Comparing arrays

Post by [-hh] » Tue Oct 04, 2016 11:48 pm

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.
shiftLock happens

Jeffrey P
Posts: 2
Joined: Mon Mar 11, 2013 11:38 pm

Re: Comparing arrays

Post by Jeffrey P » Wed Oct 05, 2016 5:29 pm

Thank you, that gives me some ideas.

Post Reply