Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array subtraction

Status
Not open for further replies.

mellenburg11

Programmer
Aug 30, 2004
9
US
I have two arrays, A and B. B is a subset of A. I would like to create array C that contains the elements of A that are not in B. How do I do this?
 
for apos=0 to ubound(a)
aValueFound = False
for bpos=0 to ubound(b)
If a(apos) = b(bpos) then
aValueFound = True
exit for
End If
next
If Not aValueFound Then
cvalues = cvalues & apos & "," ' assigns bookmarks
End If
next
if cvalues <> "" then
cvalues = left(cvalues, len(cvalues)-1) ' stripping the trailing comma off
cArr = split(cvalues, ",")
Dim C
Redim C(ubound(cArr))
for cpos=0 to ubound(cArr)
C(cpos) = a(cArr(cpos)) ' assigns the value from the bookmark
Next
Else
C = Split("",",")
End If

verbose breakdown :
does a nested loop for a and b, flagging mathing instances of a and b ( if you'd like a matching list, move the cvalues = cvalues line inside the conditional block inside the nested loop )
checks for failed match, if failed to match, adds the array bookmark to a string array, builds the string array..
checks for content of the string array, strips the trailing comma, splits it into an array, dims C , redims it for the proper size, then loops the string array and populates the C array with the values from A

otherwise it creates an empty C array.

[thumbsup2]DreX
aKa - Robert
 
You know me, I hate extra for loops and all that, using some string functions we may be able to increase it's performance, though it could as easilt decreas it, not sure what your working with:
Code:
Function ArraySubtract(arrayA,arrayB)
   Dim bContents, delim
   delim = "##DELIM##"
   bContents = delim & Join(arrayB,delim) & delim

   Dim subContents, ctrA
   For ctrA = 0 to UBound(arrayA)
      If InStr(bContents,delim & arrayA(ctrA) & delim) > 0 Then subContents = subCOntents & arrayA & delim
   Next
   If len(subContents) > 0 Then subContents = Left(subContents,Len(subContents) - len(delim))

   ArraySubtract = Split(subContacts, delim)
End Function

Again, this may or may not provide better performance since it cuts down on the number of for loops. The various string conversions and concatenations may hurt you a bit but I think it will be more efficinct then the ReDim's and multipe for loops.

My other solution requires some custom classes that may be more complicated then what you want, but basically it would build a binary tree using the first array's values. Then it would delete nodes from that tree based on the second arrays values, the final step would be to take the remainaing nodes and return them as an a array, thus giving you the subtraction. I dunno, maybe i haven't had enough coffee yet :p

-T

barcode_1.gif
 
It's funny, I was going to suggest the very same thing, Tarwn, but had the same concerns about the string manipulation and decided to save myself the effort. :) Nice job.
 
Well, I was concerned to, mostly with the concatenations in the for loop, the Join and Split ought to be fairly efficient (compared to concatenations). I dunno, I'll run the two functions through a benchmark later and post the results.

-T

barcode_1.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top