使用CSV文件计算数组

时间:2011-12-04 18:42:44

标签: vb.net arrays csv

我正在尝试接收和解析CSV文件。现在我接受它并产生这样的东西:

enter image description here

程序加载csv并将数据复制到数组中:

ReDim strarray(num_rows, num_cols)
For x = 0 To num_rows
  strline = strlines(x).Split(",")
  For y = 0 To num_cols
    strarray(x, y) = strline(y)
  Next
Next

CSV文件数据是基本格式化的,有两列和x行:

212, 343
324, 232  

等。我想我的主要问题是尝试对特定列中的所有值执行计算。首先,我只想弄清楚如何隔离列,并发现通过使用MsgBox(strarray(x,num_cols)),它将msgbox第二列中的所有内容两次。我只想尝试理解如何执行基本计算,例如将第一列中的每个值乘以2,将第二列中的每个值乘以3。

1 个答案:

答案 0 :(得分:0)

首先:在VB数组中,从0到项目数减去1.然而,您将指定最大索引,而不是大小:

Dim x As String() = new String(N-1) 'Where N is the number of items.
Dim y As String() = new String(MAX) 'Where MAX is the highest index.

你有整数。所以你必须声明:

Dim matrix As Integer(,) = new Integer(num_rows-1, num_cols-1)

然后填写:

For row As Integer = 0 To num_rows-1
    Dim strline As String() = strlines(row).Split(",")
    For col As Integer = 0 To num_cols-1
        matrix(row, col) = Integer.Parse(strline(col))
    Next                 
Next 

示例计算:

For row As Integer = 0 To num_rows-1
    matrix(row,0) *= 2
    matrix(row,1) *= 3
Next
相关问题