Compare the number of elements in two different arrays

V

Varun

Guys,

I have two arrays, namely A and B - both have been dimensioned as follows
for unavoidable reasons:

Dim A(1 to 100) As Variant
Dim B(1 to 100) As Variant

The arrays have different number of elements in them i.e. A has 4 elements,
as in
A(1), A(2), A(3), A(4) and B has 2 elements, as in B(1), B(2). Note that
since I have declared both arrays from 1 to 100, A(5)..A(100) = "" and
likewise B(3)...B(100 = "" i.e. rest aray values are empty strings.

How do I compare the number of elements? i.e. since A has 4 non empty
elements and B has 2 non empty elements, I want to perform this check.

Any pointers are appreciated. Thanks.
 
R

Rick Rothstein

As long as your arrays are declared as either String or Variant, you can use
this function to count the number of elements with values assigned to them
(whether those elements are sequentially assigned or not)...

Function NumberOfUsedElements(ArrayIn As Variant) As Long
Dim Elements As String
If VarType(ArrayIn) = vbArray + vbString Or _
VarType(ArrayIn) = vbArray + vbVariant Then
Elements = Join(ArrayIn, Chr(1))
Do While InStr(Elements, Chr(1) & Chr(1))
Elements = Replace(Elements, Chr(1) & Chr(1), Chr(1))
Loop
If Left(Elements, 1) = Chr(1) Then Elements = Mid(Elements, 2)
If Right(Elements, 1) = Chr(1) Then Elements = Left(Elements, _
Len(Elements) - 1)
NumberOfUsedElements = UBound(Split(Elements, Chr(1))) + 1
End If
End Function
 
S

Sheeloo

It depends on the purpose of your comparison...

Normally you write a loop which 'compares' corresponding elements...

In this case you can try something like
i = 1
While (A(i) <> "" Or B(i) <> "")
'...Compare
i = i + 1
Wend
 
V

Varun

Thanks Guys.

I do not want to compare every element of the arrays. Instead, I want to
compare the "number" of elements in the two arrays.

How would I do that.

Thanks.
 
R

Rick Rothstein

The function I gave you will return the number of elements in a String or
Variant array passed into it... if I understand what you want correctly,
just pass each array into it separately and add the results together...

TotalInUseElements = NumberOfUsedElements(A) + NumberOfUsedElements(B)
 
S

Sheeloo

You can fine the total number of elements in the array by
noOfElements = UBound(A) - LBound(A)

Following will give you the count of elements <> ""
i=0
For j = LBound(A) to UBound(A)
i = i + 1
Next
 
D

Dave Peterson

If I were you I'd keep track of what was used when I added things to them.

Dim aCtr as long
dim bCtr as long
dim A(1 to 100) as variant
dim B(1 to 100) as variant

actr = 0
bctr = 0

'some kind of check to add elements to A
for each something in something.things
if something is true then
actr = actr + 1
a(actr) = something
end if
next something

'some kind of check to add elements to B
for each something in something.things
if something is true then
bctr = bctr + 1
b(actr) = something
end if
next something

if actr = 0 then
'nothing added to A
else
redim preserve A(1 to actr)
end if

if bctr = 0 then
'nothing added to B
else
redim preserve B(1 to bctr)
end if

if actr = bctr then
'same number of elements (may be 0, too!)
else
'different number
end if
 
B

Bernd P

Hello Dave,

Hmm, and if he often deletes random elements? Then your approach seems
to be dying with garbage collection (= rearranging elements), right?

Regards,
Bernd
 
V

Varun

Dave and All, thanks very much for the pointers. For now, I'll go with
Dave's trick. Thanks again.
 
D

Dave Peterson

If elements are deleted, one could define a new array, reorganize into that and
erase the original.

But to be honest, I can't remember do that kind of thing as often as inspecting
some kind of list and extracting just the things I wanted to create my array.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top