Solved

# search an array

Posted on 2006-05-28
179 Views
hi experts,

is there a way to search for a string inside an array in vb6.

sample array:
arr(0) = "A"
arr(1) = "B"
arr(2) = "C"
arr(3) = "D"

i want to search for C and return 2.

thanks, would appreciate any ideas/thoughts on this.

regards.
ann

0
Question by:wala_lang

LVL 142

Accepted Solution

function getindex(arr, value)
dim lngIndex as long

for lngIndex = lbound(arr) to ubound(arr)
if arr(lngIndex)= "C" then
getindex = lngindex
exit function
end if
next
getindex = lbound(arr) -1
end function

0

LVL 8

Expert Comment

Sub findString()
Dim arr(5) As String

arr(0) = "A"
arr(1) = "B"
arr(2) = "C"
arr(3) = "D"

phrase = InputBox("enter a search phrase")

endofarray = UBound(arr) - LBound(arr) - 1

y = 0

Do While (y < endofarray)
If (arr(y) = phrase) Then
MsgBox "string found in element " & y
GoTo skippy
End If

y = y + 1

If (y >= endofarray) Then
End If
Loop

skippy: End Sub
0

LVL 8

Expert Comment

Sub findString()
Dim arr(5) As String                           'notice how the array is larger (0-5) than the number of values it has (0-3)

arr(0) = "A"
arr(1) = "B"
arr(2) = "C"
arr(3) = "D"

phrase = InputBox("enter a search phrase")

endofarray = UBound(arr) - LBound(arr) - 1         'This will make sure only 0-3 get searched

y = 0                                                                'this is the loop counter

Do While (y < endofarray)
If (arr(y) = phrase) Then
MsgBox "string found in element " & y
GoTo skippy
End If

y = y + 1                                         'if the phrase isn't found, check the next index

If (y >= endofarray) Then                 'if the next index is greater than the number of indexes, end the function
End If
Loop

skippy: End Sub
0

LVL 19

Expert Comment

Modified angleIII's code to make use of the 'value' as he calls it parameter.

Public Function searchArray(ByRef myArray() as String, ByVal strSearch as String) as Integer
dim lngIndex as Long
For lngIndex = LBound(Array) to UBound(myArray)
If myArray(lngIndex) = strSearch Then
searchArray = lngIndex
Exit Function
End If
Next lngIndex
searchArray = -1
End Function
0

Author Comment

thanks a lot for your time experts.  i will award the points to angel coz i think its all the same logic.  loop thru the array and exit the loop once you found a match.  and angel was the first one to respond.

i was kinda hoping that there's another logic instead of looping coz i've looked around over the net and they're all the same.

looks like there's no short-cut at all.

anyway, this will do.

appreciate the time guys!!!!

ann
0

LVL 19

Expert Comment

>>looks like there's no short-cut at all.

Infact there is a shortcut, its called a HASH TABLE. However, this would require the use of another array.

0

LVL 8

Expert Comment

how to do it without a loop: http://www.devx.com/vb2themax/Tip/18364
0

LVL 19

Expert Comment

YoungBonzi : Perhaps we should talk about what the join function does:

Here is code for a join function:
for i = lbound(array) to ubound(arr)
output = output & delimeter & array(i)
next i

Perhaps you can see that all the join function does is traverse the array and append them to a string (A memory allocation performed each time?) Then using that method in the link it has to use the slow string searching functions (namely InStr()) to determine if the string is present.

Then finally and most important, this will only tell you yes or no if the item exists in the array. The questioneer wanted to get the INDEX of where the item was.
0

LVL 19

Expert Comment

Also, I dont know the numerical representation of VbNullChar, i'm going to assume that its 0. If so, this could be bad if the data is ever going to be used outside of this program. For example:

Msgbox Join(Array,VbNullChar) '''WHERE DID THE REST GO??

Chr(0) is a null terminator, it ends strings.

Brian
0

## Featured Post

### Suggested Solutions

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…