从CSV填充2D数组(没有m * n-Loops)

时间:2016-09-07 11:50:49

标签: arrays vb.net

在将Excel-VBA项目迁移到Visual Basic 2010时,我在填充数组时遇到了问题。

在Excel-VBA中我会做类似

的事情
Function mtxCorrel() As Variant
    mtxCorrel = wsCorr.UsedRange
End Function

m*n - 矩阵(在本例中为n*n)(方便地存储在工作表中)读入数组以供进一步使用。

在VB2010中,我显然不会使用Excel-Worksheet作为存储。 csv-Files(见下文)似乎是一个不错的选择。

我想用csv-contents 填充二维数组而不用循环n*n - 次。我们假设我已经知道n=4用于演示目的。

This表明我想做的事情无法完成。

尽管如此,我仍然希望以下内容可以起作用:

Function mtxCorrel() As Object

    Dim array1(4, 4) As String

    Using ioReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("C:\cm_KoMa.csv")

        With ioReader
            .TextFieldType = FileIO.FieldType.Delimited
            .SetDelimiters(";")

          ' Here I want to...
          ' A) ...either populate the whole 2d-array with something like
            array1 = .ReadToEnd()

          ' B) ... or populate the array by looping its 1d-"rows"
            While Not .EndOfData
                array1(.LineNumber, 0)= .ReadFields()
            End While

        End With
    End Using

    return array1
End Function

注意:

  • 我主要对填充阵列感兴趣。
  • 我对确定哪个csv-line属于哪个1d - " row"以及对检查n不感兴趣的潜在错误不太感兴趣。

附录:示例csv-File:

1;0.5;0.9;0.3
0.5;1;0.6;0.2
0.9;0.6;1;0.1
0.3;0.2;0.1;1

0 个答案:

没有答案