Dman
Dman

Reputation: 573

Read Staggerd List into MultiDimensional Array

I have a staggered list saved as a text file, and I need to read that file into a multi dimensional array, where the header is in the first level, and all lines under it are the second level.

EX:

Greetings
    Hello
    How are you
    Have a great day
Needs
    Help
    I need a drink

Array(0,0) = Greetings

Array(0,1) = Hello

Array(0,2) = How are you

How can I loop through this information and know when it reaches the second column?

Upvotes: 0

Views: 39

Answers (1)

Blackwood
Blackwood

Reputation: 4534

It would be easier to uses lists rather than arrays for this. However, the following code will create a ragged array that represents your categories.

Dim lists As New List(Of String())
Dim currentGroup As New List(Of String)
For Each line As String In IO.File.ReadAllLines(filePath)
    If line.StartsWith(" ") Then
        currentGroup.Add(line.Trim)
    Else
        If currentGroup.Count > 0 Then lists.Add(currentGroup.ToArray)
        currentGroup = New List(Of String) From {line}
    End If
Next
If currentGroup.Count > 0 Then lists.Add(currentGroup.ToArray)
Dim myArray()() As String = lists.ToArray

Upvotes: 1

Related Questions