We help IT Professionals succeed at work.

'Search Next' function in an array?

NO_CARRIER
NO_CARRIER asked
on
Medium Priority
242 Views
Last Modified: 2010-05-01
I'm populating an array from a text file..

I'm performing a search on this array... with:
Open MyFile For Input As #1
   Do Until EOF(1)
   If StrArray(1) = Text1.text then
          ....
   End if
Loop
Close

however, there can possibly be more than one match in the column, how can I perform a "Find Next"?





Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2007

Commented:
Greetings NO_CARRIER!
Can you provide an example of the file?
Odds are you can use
 Line Input #1, strArray(1)
to pull in the full string, but I may be missing your point here..? :D
~sirbounty

Author

Commented:
Sure...

code:

MyFile = "c:\test.txt"

Open MyFile For Input As #1
   Do Until EOF(1)
      Line Input #1, LineText
      strArray=Split(LineText, "/")
   If StrArray(1) = Text1.text then
          ....
   End if
Loop
Close


file (test.txt)

1/Hello
2/Marker
3/Pencil
4/Dog
5/Cat
1/World



--- notice the number "1" is there twice.  (once with Hello, again with World).  When I perform a search for the number "1" .. it returns "Hello".  If I click the Search button a second time, I'd like it to return "World" ... or any other instances of number "1" each time I click Search.


I hope this helps?
CERTIFIED EXPERT
Top Expert 2007

Commented:
How about this way instead?

 Do Until EOF(1)
      Line Input #1, LineText
      if instr(1,LineText,Text1.text) > 0 then

Author

Commented:
sorry, in that code...

the .... should be: MsgBox strArray(2)
CERTIFIED EXPERT
Top Expert 2007

Commented:
NO_CARRIER,
Didn't have my window opened up enough to read the tail-end of that...sorry!
But, just slightly modify the above:


Do Until EOF(1)
      Line Input #1, LineText
      FullText=LineText & ","
Loop
      if instr(1,FullText,Text1.text) > 0 then...
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2007

Commented:
Open ("C:\test.txt") For Input As #1
Do Until EOF(1)
    Line Input #1, LineText
    fulltext = LineText & "," & fulltext
Loop
x = 1
Do While x <> 0
    strFind = Mid(fulltext, x)
    strFind = Mid(fulltext, x, (InStr(strFind, ",")) - 1)
    strreply = strFind & vbCrLf & strreply
    x = x + 1
    x = InStr(x, fulltext, Text1.Text)
Loop
'And if you didn't want the Msgbox in the format of
'   1/Hello
'   1/World
' You can always split it...
'strResults = Split(strreply, "/")

End Sub
this is simple.. all you need to do is:


Dim xArr() As String
Dim xText As String
Open TheFile For Input As #1
While Not EOF(1)
Line Input #1, xText
xArr() = Split(xText, "/")
If xArr(0) = Text1.Text Then
'whatever u wanna do with it
MsgBox xArr(1)
End If
Wend
Close #1

Commented:
There are a few ways you can handle this.

1. You can read the entire file and have the search actually looking through a cached string of the entire file, from which you can easily move forward and backwards.

2.  You can open the file for random access and keep track of where you were last, thus allowing you to seek back to that starting point and continue searching from there.

3.  You can move your file open to a module level and have the search only deal with searching from where it left off last.

If you need to start over you could close the file if your search field were changed, or you could provide a button that would allow the user to choose refresh and it would close the file and reopen it at the beginning to handle the next search.

Commented:
You should just buffer the file (sArray) and keep a track on where the last number was found (lPos). Example (Form1):

Option Explicit

Private sArray() As String, lPos As Long
Private Function ArrayLoad(ByVal sFile As String) As Boolean
    Dim sBuff As String, iFF As Integer
    If Dir(sFile, vbNormal + vbHidden) <> vbNullString Then
        iFF = FreeFile
        Open sFile For Binary As iFF
            sBuff = Space(LOF(iFF))
            Get #iFF, 1, sBuff
        Close iFF
        sArray() = Split(sBuff, vbCrLf)
        ArrayLoad = True
    End If
End Function
Private Function ArraySearch(ByVal bFindNext As Boolean, ByVal lFindNumber As Long) As Long
    Dim lLoop As Long, sSearchArray() As String
    If bFindNext = False Then lPos = 0
    For lLoop = lPos To UBound(sArray)
        sSearchArray = Split(sArray(lLoop), "/")
        If UBound(sSearchArray) = 1 Then
            If sSearchArray(0) = lFindNumber Then
                ArraySearch = lLoop
                lPos = lLoop + 1
                Exit Function
            End If
        End If
    Next lLoop
    ArraySearch = -1
End Function
Private Sub Command1_Click(Index As Integer)
    Dim lRet As Long
    '2 Command buttons.. Index 0 and Index 1
    'Index 0 = Find
    'Index 1 = Find Next
    lRet = ArraySearch(IIf(Index = 0, False, True), 1)
    If lRet > -1 Then
        MsgBox sArray(lRet)
    Else
        MsgBox "Could not find that number."
    End If
End Sub
Private Sub Form_Load()
    If ArrayLoad("c:\test.txt") = False Then
        Call MsgBox("Could not load file.")
    End If
End Sub
NO_CARRIER:

Look at VB function Filter. It searches for some match in string array.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.