Count matching elements in an array.

# Thread: Count matching elements in an array.

1. Junior Member
Join Date
Dec 1969
Posts
3

## Count matching elements in an array.

In an array, for example, numArray(10,48,63,63,63,70) how can i count how many times a number (or string) appears in an array?<BR><BR>In this example, i am creating an array from a comma delimited field in a database, each element refers to a goalscorer in a football match. Each element is then checked against another array to change the number to a surname.<BR><BR>I want the output to read a single string that looks like:<BR><BR>Jones, Smith, Clarke (3), Williams<BR><BR>Many thanks

2. Senior Member
Join Date
Dec 1969
Posts
623

do read about normalization concepts and why never to have comma delimited lists in a field<BR><BR>anyway, you can have a two dimensional array, one to store the name of player and the second to store the count, you can have a function that looks like this<BR>*************************<BR>function addToArray(name)<BR> dim theIndex<BR> theIndex = -1<BR> theLength = UBound(myArray, 2)<BR> for iLoop = 0 to (theLength - 1)<BR> if myArray(0, iLoop) = name then<BR> theIndex = iLoop<BR> end if<BR> next<BR> if theIndex = -1 then<BR> myArray(0, theLength) = name<BR> myArray(1, theLength) = 1<BR> else<BR> myArray(1, theIndex) = myArray(1, theIndex) + 1<BR> end if<BR>end function<BR>*****************************<BR><BR>s o if there is no such name in the array, it will add it with count = 1, but if there is a similiar name in the array it just increments the count....<BR><BR>the best way is *normalization of the db*

3. Junior Member
Join Date
Dec 1969
Posts
3