After a bit of thinking I concluded that the quickest way to do this would be to create a clone of each array, delete any common items from both arrays (after copying to a third array to hold common items) and you are left with the differences and the intersection. Here's what I've got so far:
Code: Select all
#NoEnv
#SingleInstance Force
/*
Common: Apple Banana Melon
Array_A: Pear Orange Walnut Almond
Array_B: Peanut Cashew Grape Kiwi
*/
Array_A := ["Apple", "Pear", "Banana", "Orange", "Melon", "Walnut", "Almond"]
Array_B := ["Peanut", "Apple", "Melon", "Cashew", "Banana", "Grape", "Kiwi"]
MyArray := Array_Compare(Array_A, Array_B)
For KeyCompare, ArrayCompare in MyArray
{
Text .= KeyCompare ":"
For Key, Item In ArrayCompare
Text .= " " Item
Text .= "`n"
}
MsgBox %Text%
Return
Array_Compare(Obj1, Obj2)
{
Comparison := {}
Comparison.Obj1 := Obj1.Clone()
Comparison.Obj2 := Obj2.Clone()
Comparison.Common := {}
;Note: Once the object is enumerated, you don't need to worry about whether items are deleted or not.
For Key1, Item1 In Comparison.Obj1
{
For Key2, Item2 In Comparison.Obj2
{
If (Item1 = Item2)
{
Comparison.Common.Insert(Item1)
Comparison.Obj1.Remove(Key1)
Comparison.Obj2.Remove(Key2)
Break
}
}
}
Return Comparison
}
I find my code tends to be very "verbose" and I often find people here on the forums who come up with smarter ways of doing the same thing, so I am wondering if there is a better way of comparing arrays. Thanks!