Ace Master Posted December 15, 2003 Posted December 15, 2003 I know how to read a text file and put elements to one string. But I want to put the text file into a bidimentional array like this: array(10,3) where 10 is the line numbers from the text file, and 3 are the columns. Each element is separated by "tab" I can't find anywhere any method for doing this. any ideeas ? thanks Quote
Ace Master Posted December 15, 2003 Author Posted December 15, 2003 I write this code for reading lines one by one and put them into the line object. But I want now to split every line and put them into the 2d array, and don't know how, because the split don't wotk on my variables Dim Read As New System.IO.StreamReader("test.txt") Dim Line(60) As Object Dim x As Integer For x = 0 To 60 Line(x) = Read.ReadLine Next x thanks Quote
*Experts* Bucky Posted December 15, 2003 *Experts* Posted December 15, 2003 Arrays in VB.NET have, by default, a starting index of 0. This means that your array declaration will actually be array(9, 2). Now, the code to split up the line into multiple parts... you want to use System.Text.RegularExpressions.RegEx.Split (catchy name, huh?): Dim myArray(9, 2) As String Dim x As Integer Dim Read As New System.IO.StreamReader("test.txt") For x = 0 To 9 ' Loop ten times Dim line As String = Read.ReadLine() ' Read the line Dim values() = System.Text.RegularExpressions.RegEx.Split(line, ControlChars.Tab) ' Split up the line myArray(x, 0) = values(0) myArray(x, 1) = values(1) myArray(x, 2) = values(2) Next x Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
Ace Master Posted December 16, 2003 Author Posted December 16, 2003 thank you so much !!!!!!! :) Quote
Ace Master Posted December 16, 2003 Author Posted December 16, 2003 It is possible that the bounds of the array to be declared dynamically by the file length? What I mean is that maybe my file will have 30 lines and 6 columns. I don�t know what my file will look like, and that�s way I�m asking. Thanks Quote
Jay1b Posted December 16, 2003 Posted December 16, 2003 yeah y can use redim preserve to reset the amount of lines in the array. put it in the loop setting it to an incrementing integer. also ur using dim line(60) as object Unless theres some other reason for having the object in there u should use integer. Quote
Ace Master Posted December 16, 2003 Author Posted December 16, 2003 (edited) The reason I want that is because I will put other values in the matrix. Let's say that I have an 2d array with 15 lines and 4 columns I make some calculations and I put values in each line and I wil have then 6 columns in my array. So I need to have an array with 6 columns even if those columns values are empty. or... I want to redim the array bounds by how many lines the file has, but don't know how to make this. Edited December 16, 2003 by Ace Master Quote
Ace Master Posted December 16, 2003 Author Posted December 16, 2003 I made those two 2D arrays works perfectly and to take info from one and put to the other one. (+ 2 columns) The problem still is that I don't know how many lines has the text file, and i need that for my loop . I made the loop by number of lines from the file. If the loop is bigger then the lines I will have an error. Quote
*Experts* Bucky Posted December 16, 2003 *Experts* Posted December 16, 2003 Instead of using a For loop in which you define the starting and ending values, use a Do loop which you can exit whenever you want. However, you can only resize the last dimension of a multidimensional array, so you could not resize the array here to fit the number of lines in your file. I suggest you look at using either an [mshelp=ms-help://MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemcollectionsarraylistclasstopic.htm]ArrayList[/mshelp] that contains a collection of [mshelp=ms-help://MS.MSDNQTR.2003FEB.1033/vblr7/html/vastmStructure.htm]Structures[/mshelp] to store your data, or dynamically creating a [mshelp=ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpcontheadonetdataset.htm]DataSet[/mshelp] to store the data. This fits your scenario much better. Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.