Notifications
Clear all
[Closed] fn DifferenceArrays ArrayOne ArrayTwo
Jul 10, 2008 11:27 pm
I wrote an array difference function which seems to be working. Anybody see a reason why it shouldn’t work?
fn DifferenceArrays ArrayOne ArrayTwo =
(
Diff = for o in ArrayOne where (finditem ArrayTwo o) == 0 collect o
join Diff (for o in ArrayTwo where (finditem ArrayOne o) == 0 collect o)
ArrayOneS = for a in ArrayOne collect a as string
ArrayTwoS = for a in ArrayTwo collect a as string
for i = Diff.count to 1 by -1 where (a = finditem ArrayOneS (Diff[i] as string)) > 0 and (b = finditem ArrayTwoS (Diff[i] as string)) > 0 do
(
if (classof ArrayOne[a] == classof ArrayTwo[b]) do
(
deleteitem Diff i
)
)
return diff
)
EDIT: Added Classof check to ensure that one wasn’t already a string version of a different class.
1 Reply
Jul 10, 2008 11:27 pm
Figured that I might want just the difference of one array instead of two. So I split it up into two function and created a “union” function which operates on the same general principle.
fn getArrayDifference ArrayOne ArrayTwo =
(
Diff = for o in ArrayOne where (finditem ArrayTwo o) == 0 collect o
ArrayTwoS = for a in ArrayTwo collect a as string
for i = Diff.count to 1 by -1 where (b = finditem ArrayTwoS (Diff[i] as string)) > 0 do
(
if (classof Diff[i] == classof ArrayTwo[b]) do
(
deleteitem Diff i
)
)
return diff
)
fn getAllArrayDifference ArrayOne ArrayTwo =
(
Diff = for o in ArrayOne where (finditem ArrayTwo o) == 0 collect o
join Diff (for o in ArrayTwo where (finditem ArrayOne o) == 0 collect o)
ArrayOneS = for a in ArrayOne collect a as string
ArrayTwoS = for a in ArrayTwo collect a as string
for i = Diff.count to 1 by -1 where (a = finditem ArrayOneS (Diff[i] as string)) > 0 and (b = finditem ArrayTwoS (Diff[i] as string)) > 0 do
(
if (classof ArrayOne[a] == classof ArrayTwo[b]) do
(
deleteitem Diff i
)
)
return diff
)
fn getArrayUnion ArrayOne ArrayTwo =
(
UnionArray = #()
Diff = #()
for o in ArrayOne do
(
if (finditem ArrayTwo o) != 0 then
(
append UnionArray o
)
else
(
append Diff o
)
)
ArrayTwoS = for a in ArrayTwo collect a as string
for i = 1 to Diff.count where (b = finditem ArrayTwoS (Diff[i] as string)) > 0 do
(
if (classof Diff[i] == classof ArrayTwo[b]) do
(
append UnionArray Diff[i]
)
)
Return UnionArray
)