Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting items in a multi-dimensional array

If I have the following array:

    Dim Array(4, 10) As String
    Array(0, 0) = "100"
    Array(0, 1) = "200"
    Array(1, 0) = "300"
    Array(1, 1) = "400"
    Array(1, 2) = "500"
    Array(1, 3) = "600"

How do I get the following count:

0 = 2
1 = 4
like image 294
oshirowanen Avatar asked Feb 25 '23 22:02

oshirowanen


1 Answers

It sounds like you're trying to count the number of non-Nothing values in each dimension of the array. The following function will allow you to do that

Public Function CountNonNothing(ByVal data As String(,), ByVal index As Integer) As Integer
    Dim count = 0
    For j = 0 To data.GetLength(1) - 1
        If data(index, j) IsNot Nothing Then
            count += 1
        End If
    Next
    Return count
End Function

And it can be invoked like so

Dim count1 = CountNonNothing(Array, 0)
Dim count2 = CountNonNothing(Array, 1)
like image 158
JaredPar Avatar answered Mar 07 '23 10:03

JaredPar