Populate Array from delimited file?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Does anyone know of an easy way to populate an array from a delimited text file when you don't know the number of columns ahead of time?

I've been trying all day to use a combination of String.Split and the arraylist.add and the best I've managed to do is create an array of arrays. I searched Google and some people recommending using the Oledbprovider for Jet but that seems a bit hokey to me.
 
Dim fs As New StreamReader("c:\test\array.txt")
Dim myarr() As String
Dim MyStr As String = fs.ReadToEnd()
fs.Close()
myarr = MyStr.Split(Chr(58)) ' my sample delimiter char was a colon ":"
For i As Integer = 0 To myarr.Length - 1
Debug.WriteLine(myarr(i))
Next
 
That doesn't handle rows. If there are 3 rows with 3 columns in a file, that solution returns an array with one dimension and 9 elements rather than a 3 x 3 matrix. I need some way of cycling through each line and appending the elements from each line intot he array. I know I can find a way to code that with redims but I was hoping for a function in the CLR.

I'll code the redim solution and post it in a little bit.
 
Dim fs as New StreamReader("c:\test\junk\array.txt")
Dim Str as String
Dim sStartTag As String = "<item>"
Dim sEndTag As String = "</item>"
Dim xmlMatrix As String
Dim dom As New Xml.XmlDocument
xmlMatrix = "<MyMatrix>"
Do While fs.Peek >= 0
Str = fs.ReadLine
Str = Replace(Str, Chr(58), sEndTag + sStartTag) ' assume chr(58) = ":" is the delimiter
Str = "<row>" + sStartTag + Str + sEndTag + "</row>"
'Debug.WriteLine(Str)
xmlMatrix += Str
Loop
fs.Close()
xmlMatrix &= "</MyMatrix>"
Debug.WriteLine(xmlMatrix)
dom.LoadXml(xmlMatrix)
dom.CreateXmlDeclaration("1.0", "UTF-8", "yes")
dom.Save("c:\test\junk\Myarray.xml")

'do whatever with the xml. load a dataset..bla bla bla..
 
Back
Top