1

I am given this .txt file.

Left Behind,Lahaye,F,7,11.25
A Tale of Two Cities,Dickens,F,100,8.24
Hang a Thousand Trees with Ribbons,Rinaldi,F,30,16.79

That file is: Book Title, Author ,Fiction or Nonfiction, Stock ,Price

I need to split those into multiple arrays, at least I feel like I do, I have this so far

    Private Sub frmInventory_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'Populate array
    Dim temp() As String = IO.File.ReadAllLines("Books.txt")
    temp = Split(",")
    lstBooks.DataSource = temp.ToList
End Sub

which isn't doing the trick... obviously.

I just don't know how to make it put it into multiple arrays, such as book, etc. or maybe a 2 dimensional array.

Thanks for any help.

I have looked on many sites like this, but the only help they offer is splitting it in 2. http://patorjk.com/programming/tutorials/vbarrays.htm#splitfunction

2 Answers 2

2

If the format is always so strict, you can do it manually with string.Split, otherwise i would suggest to use an existing library like FileHelpers, this fast CSV parser or the VB.NET onboard TextFieldParser class.

To answer your actual question, you can use an File.ReadLines and an IEnumerable(Of String()).

Dim lines As IEnumerable(Of String()) = 
         From line In IO.File.ReadLines("Books.txt")
         Select line.Split(","c)

If you want an array: lines.ToArray(), that will load all into memory(like File.ReadAllLines) whereas File.ReadLines streams the lines from the file and only if you ask for(f.e. via Take(10)).

Edit: If you want the most reusable approach, use a custom class with these properties and initialize it from the string():

Public Class Book
    Public Property Title As String
    Public Property Author As String
    ' and so on '
End Class

 Dim books = From line In System.IO.File.ReadLines("Books.txt")
             Let parts = line.Split(","c)
             Select New Book() With {
                .Title = parts(0),
                .Author = parts(1)
            }

You can use this for example in a For Each or as DataSource. Note that it is error-prone, the title probably contains also commas or the format is not always strict.

Sign up to request clarification or add additional context in comments.

Comments

0

Without knowing what you want to do with the data, its hard to help. But i imagine your best bet would be to create a book class, with properties for Book Title, Author ,Price etc.

Create a list(of book), read the txt file line by line, splitting each line on commas, and creating a book instance with each resulting array, and add it to the list, like this hastily written example:

Public Class book
Property title As String
Property author As String
Property price As Double

Public Sub New(ByVal title As String, ByVal author As String, ByVal price As Double)
    _title = title
    _author = author
    _price = price
End Sub
End Class

Public Class Form1
Public booklist As List(Of book)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)     Handles Button1.Click
    Dim bookfile As String() = IO.File.ReadAllLines("c:\file.txt")
    For Each line As String In bookfile
        Dim bookinfo As String() = line.Split(",")
        Dim abook As New book(bookinfo(0), bookinfo(1), bookinfo(2))
        booklist.Add(abook)
    Next
End Sub
End Class

If this is the kind of thing you would like to do, i will improve the above code as needed

2 Comments

I appreciate your answer! but basically all I'm trying to do with the data is use the arrays I get to make the data sortable, so I could say if author(1) is Lahaye then Author(1) = ("Lahi") or something like that That's why I'm trying to split it into multiple arrays
Ok, im finding it hard to understand what you want to achieve. Perhaps you could edit your question to give the big picture, because it still seems like you just want to sort the data, and a list(of object) seems to be the simplest option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.