Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 192
  • Last Modified:

search an array

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
wala_lang
Asked:
wala_lang
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:

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
 
YoungBonziCommented:
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
        MsgBox "string not found"
    End If
Loop

skippy: End Sub
0
 
YoungBonziCommented:
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
        MsgBox "string not found"
    End If
Loop                                                

skippy: End Sub
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
BrianGEFF719Commented:
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
 
wala_langAuthor Commented:
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
 
BrianGEFF719Commented:
>>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
 
YoungBonziCommented:
how to do it without a loop: http://www.devx.com/vb2themax/Tip/18364
0
 
BrianGEFF719Commented:
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
 
BrianGEFF719Commented:
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now