Results 1 to 8 of 8

Thread: delimited text file into array()

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    12

    Question delimited text file into array()

    Hi guys, I have a text file with for example this data in it :


    "19 Mar 2010","6:00","16:00","10","1","9","6.61","£59.49¬"
    "20 Mar 2010","9:45","19:15","10","1","9","6.61","£69.89¬"
    "21 Mar 2010","9:00","17:00","8","1","8","6.61","£99.32¬"


    I used " , " to delimit individual pieces of data and " ¬ " to denote end of line.

    I'd like to be able to split the files into lines and assign each line to an array then split each of those array strings into the comma " , " delimited data and feed each piece of data into another array ready for writing to a datagridview.

    I've spent a while trying to get Split() to work but I get errors regarding converting strings to one-dimensional arrays ?

    Also the Split() method asks for a "ParamArray" but I haven't been able to find examples of other people providing them on the net any idea what those are about?


    Hope I've made sense ??

    Thanks in advance....

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

    Re: delimited text file into array()

    Look at using a TextFieldParser. The documentation contains code examples.
    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
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: delimited text file into array()

    Code:
            Dim ts As String = "19 Mar 2010,6:00,16:00,10,1,9,6.61,£59.49¬20 Mar 2010,9:45,19:15,10,1,9,6.61,£69.89¬21 Mar 2010,9:00,17:00,8,1,8,6.61,£99.32¬"
            Dim l2() As String = New String() {}
            Dim splitCH() As Char = New Char() {","c, "¬"c}
            l2 = ts.Split(splitCH)
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    12

    Re: delimited text file into array()

    Thanks for your replys, textfieldparsers is just confusing me

    All I want to do is read a line of a text file, split it into an array... do whatever with that array. Then, read the NEXT line of the text file, split that to an array ... do whatever with that array and so on until i reach the end of the text file.

    Is there an answer as simple as I've described?

    Code:
            Dim ts As String = "19 Mar 2010,6:00,16:00,10,1,9,6.61,£59.49¬20 Mar 2010,9:45,19:15,10,1,9,6.61,£69.89¬21 Mar 2010,9:00,17:00,8,1,8,6.61,£99.32¬"
            Dim l2() As String = New String() {}
            Dim splitCH() As Char = New Char() {","c, "¬"c}
            l2 = ts.Split(splitCH)
    I'm also finding the above doesn't work for me because the variable "ts" isn't going to equal a literal string its going to be the contents of a text file I can only seem to get working with something like:

    Code:
            Dim returnValue As String()
            returnValue = My.Computer.FileSystem.ReadAllText(FileName).Split("¬")

    and when I try assigning ts = returnValue it throws an error?


    I feel like I'm going mad here

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: delimited text file into array()

    Quote Originally Posted by James_C View Post
    Thanks for your replys, textfieldparsers is just confusing me

    All I want to do is read a line of a text file, split it into an array
    Code:
            Dim ts As String = "19 Mar 2010,6:00,16:00,10,1,9,6.61,&#163;59.49&#172;20 Mar 2010,9:45,19:15,10,1,9,6.61,&#163;69.89&#172;21 Mar 2010,9:00,17:00,8,1,8,6.61,&#163;99.32&#172;"
            Dim l2() As String = New String() {}
            Dim splitCH() As Char = New Char() {","c, "&#172;"c}
            l2 = ts.Split(splitCH)
    I'm also finding the above doesn't work for me because the variable "ts" isn't going to equal a literal string ....
    What if you put that code in a function and passed the line just read to the function?

    Code:
      dim myStr() as string = foobar(line just read)
    
      public function foobar (ts as string) as string()) ' 
            Dim l2() As String = New String() {}
            Dim splitCH() As Char = New Char() {","c, "&#172;"c}
            l2 = ts.Split(splitCH)
            return l2
      end function
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    12

    Re: delimited text file into array()

    Sorry dbasnett but I just don't understand how your code works which makes it difficult for me.

    I'm not sure how I'm supposed to pass a line into the function considering I'm using .ReadAllText ?

    Can you tell me how I read line by line through a text file please ?

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: delimited text file into array()

    Pass the whole string in.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  8. #8
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: delimited text file into array()

    Code:
    '
            Dim readText As String = IO.File.ReadAllText(path)
    
            Dim myStr() As String = foobar(readText)
    
        Public Function foobar(ByVal ts As String) As String() ' 
            Dim l2() As String = New String() {}
            Dim splitCH() As Char = New Char() {","c, "¬"c}
            l2 = ts.Split(splitCH)
            Return l2
        End Function
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Tags for this Thread

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