Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Basic load file to a string(,) separated by tabs

I have a number of files, with varying sizes. But I want to accomplish the same thing with all of them, load them into a string(,).

Over the last many hours, I've searched for many variations of code similar to this with some small changes it seems, but even then I could only get a single row to load in at best:

    Dim strimport As String() = {}
    Dim strimportsplit As String(,) = {}
    Dim i As Integer = 0

    strimport = File.ReadAllLines("C:\test.txt")

    For i = 0 To strimport.Length - 1
        strimportsplit = strimport(i).Split(New Char() {vbTab}) 'This line doesn't work
    Next

This is an example of my files (only they're significantly larger):

aaa fff 0
bbb ggg 1
ccc hhh 2
ddd iii 3
eee jjj 4

This is basically how i'd want the above to load into my array from external text files:

        Dim strexample As String(,) = {{"aaa", "fff", "0"},
                                {"bbb", "ggg", "1"},
                                {"ccc", "hhh", "2"},
                                {"ddd", "iii", "3"},
                                {"eee", "jjj", "4"}}

I've even tried adding all of my tables as string(,)'s to VB manually. That works... But putting it in manually like that jumps up the filesize to ~30mb and gives me a MASSIVE performance hit. Not very ideal.

My question is, how can I load from a text file into a string(,) similar to my last example above?

Thank you very much in advance.

like image 860
Acalia Avatar asked Jan 27 '23 20:01

Acalia


1 Answers

This would be easier if you switched to a Jagged Array rather than a two-dimensional one. The issue (here) with two-dimensional arrays is that you can only access and modify one element at a time, whereas with a jagged array you can access an entire row.

A jagged array is essentially an array of arrays, and can be declared like:

Dim strimportsplit As String()()

You'd have to set its row size to that of strimport.Length to ensure that it can hold the same amount of lines:

Dim strimport As String()
Dim strimportsplit As String()()

'Dim i As Integer = 0 -- No need for this, it's declared by the loop.

strimport = File.ReadAllLines("C:\test.txt")
strimportsplit = New String(strimport.Length - 1)() {}

NOTE: The reason I use strimport.Length - 1 above is because in VB.NET you actually don't specify the length when declaring a new array, but rather the index of the last item. And since indexes start at 0 the last item will have index Length - 1.

Then inside the loop you just use i to refer to the current array (row/line) of items:

strimportsplit(i) = strimport(i).Split(New Char() {vbTab})

Accessing an item can be done like so:

'strimportsplit(row)(column)

MessageBox.Show(strimportsplit(0)(1)) 'Displays "fff".
MessageBox.Show(strimportsplit(3)(2)) 'Displays "3".

You can also access an entire row if you'd like:

Dim ThirdRow As String() = strimportsplit(2)

MessageBox.Show(ThirdRow(0)) 'Displays "ccc".
like image 68
Visual Vincent Avatar answered Feb 01 '23 23:02

Visual Vincent