Results 1 to 7 of 7

Thread: Question from a n00b...... Searching Arrays

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2009
    Posts
    9

    Question from a n00b...... Searching Arrays

    Hi Guys,

    First (and probably not the last) post.

    My Problem

    I would like to search an array for a certain text (this has been read in from a text file (42 lines) and split to an array of 11 splits successfully). I see all the data in the array with a msgbox or written to a textbox. So I know the info is in the array.

    When the text (will be from a textbox eventually, but for now is a fixed string) is found, (this is (0)), I would like to put the other parts (2), (3) etc.....into strings of their own. Obviously when not found, the search should move to the next line.

    The search text will always be in (0) of each line.

    My problem is, that I can't get a search loop working and moving to the next line until the text is found, it sticks on the first line, even if wrong (which it is!).

    Of course when the text is found, I can write (2), (3) etc to variables and exit the search.

    I know this has probably just a simple solution, but I've only been learning vb for a week now and my head is spinning at this problem, I just can't get the syntax and order right.

    Suggestions ?

    TIA
    J

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Question from a n00b...... Searching Arrays

    Perhaps you could post your current code so we can see what you're doing wrong and we can help you fix it. Otherwise we're just writing the code for you and you won't learn as much from that.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2009
    Posts
    9

    Re: Question from a n00b...... Searching Arrays

    Ok, here's what I have so far

    Public Class Form1
    Dim Towers As String = "f:\temp\towers.txt"
    Dim Seltower As String
    Dim objReader As New System.IO.StreamReader(Towers)
    Dim Towerspec As String
    Dim i As Integer


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Seltower = "Amarr Control Tower"
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim aryTextFile(11) As String
    Towers = objReader.ReadToEnd()
    aryTextFile = Towers.Split(",")

    MsgBox(Towers)
    MsgBox(aryTextFile(1))
    MsgBox(aryTextFile(2))
    MsgBox(aryTextFile(3))
    'MsgBox(Towers)


    End Sub


    End Class

    The "Seltower" is the text that I will be searching for, I've tried using If, Case and Do options, the msgbox was just to check the data was in the variables and it is...

    As none of them worked I deleted them to start afresh...

    The Towerspec is where I want to write the variables to, once found.




    J

  4. #4
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Question from a n00b...... Searching Arrays

    Do you know how to do For ... Next loops? You could use this syntax (I'll let you build on it):

    Code:
    For Each LineOfFile As String In aryTextFile
                'Check for seltower value
            Next
            'OR
            For Counter As Integer = 0 To aryTextFile.Length -1
                'Check for seltower value
            Next

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2009
    Posts
    9

    Re: Question from a n00b...... Searching Arrays

    Thanks for the tip, but didn't really help at all.... Still stuck on how to search the array for the search phrase in the array(0) at the start of each line, and when found stop and get the data from array (1) (2), then exit and move on to the next part of the programm.

    All I managed up to now is to display all the data of the array (1), (2) etc in a text box or Message Box. I can't get it to just find the one line i need and take that data.

    I've tried If, Select, Equals and others, I just can't find a way...

    My code to display the array info is:

    Imports System.IO

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim objStreamReader As StreamReader
    Dim strLine As String
    Dim s As String()
    Dim Search As String

    Search = TextBox2.Text

    objStreamReader = New System.IO.StreamReader("f:\temp\towers.txt")

    'Read the first line of text.
    strLine = objStreamReader.ReadLine

    'Continue to read until you reach the end of the file.
    Do While Not strLine Is Nothing
    s = Strings.Split(strLine, ",")
    TextBox1.Text = TextBox1.Text & s(1) & " " & s(2) & vbCrLf
    strLine = objStreamReader.ReadLine


    Loop

    'Close the file.
    objStreamReader.Close()
    Console.ReadLine()


    MsgBox(s(1) & " " & s(2))


    End Sub


    End Class

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Question from a n00b...... Searching Arrays

    Still not entirely sure of the question. You seem to have done all the pieces, so I'm not sure what's left. If I read your latest post correctly, you want to see whether the string that you put in selTower is in the first item of the array that is created when you split the row, but that would just mean adding this line:

    If s(0) = selTower Then

    but that doesn't seem like the question.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    New Member
    Join Date
    May 2009
    Posts
    9

    Re: Question from a n00b...... Searching Arrays



    Why didn't I think of that? Such a simple solution......

    I think I was putting the If option in the wrong place !

    Now to try to integrate into the main programme!

    Thanks a lot Shaggy !!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width