使用VB6在Array的值内搜索

时间:2014-04-08 08:41:24

标签: arrays csv vb6

我正在创建一个使用VB6生成报告的程序。为此,我想解析CSV file中的原始数据,并将其添加到数组中。

我有3个数组:for Array1包含字段名称; Array2代表第一个值,Array3代表第二个值。如何在Array中找到我需要的值?

**The following is sample data**
*This is the first line*

Sent Messages ULR SDC-Talisay
Sent Messages AIR MMEVAL2.globe.com.ph
Sent Messages AIA Sybase-IPX-1
Received Messages NOR Sybase-IPX-2
Peer Average Roundtrip Time DEATALISAY1.globe.com.ph
Received Messages AIR MMEVAL1.globe.com.ph
Sent Messages PUA HHSSTLC-1
Received Messages ULR MMELHG1.globe.com.ph
Received Messages NOR Syniverse-CHI
Pending Requests ULR Sybase-IPX-1
Received Messages ULR MMESJN2.globe.com.ph
Sent Messages CLR fep-Sybase-sctp
Sent Messages AIA MMETB.globe.com.ph
Pending Requests IDR MMESJN2.globe.com.ph
Roundtrip Time PUR Globe-Telstra-Telecom
Received Messages CLA fep-Local-sctp
Parsed Messages ULR
Received Messages DSA MMESJN3.globe.com.ph
Received Messages NOR Syniverse-DAL
Received Messages AIA HHSSVLO-2
Timeout Events NOR HHSSTLC-1
Received Bytes HHSSVLO-2
Received Messages AIA HHSSTSY-3
Sent Messages AIR Syniverse-CHI
Received Messages NOA Citic-MEG
Received Messages AIA HHSSTLC-4
Sent Messages AIR MMELHG1.globe.com.ph'

*Second line*

0
0
0
0
0
84.8
0
1836.6
0
0
14681.13
118.97
0
0
0
1909.6
23791.67
0
0
0
0
0
0
0
0
0
0

第三行与第一行相似,第四行与第二行相似,依此类推。

这是有计算的数据。

包含Parsed MessagesReceived MessagesReceived BytesSent BytesSent Messages的所有字段名称都应该有计算且不应计算

2 个答案:

答案 0 :(得分:1)

原始数据似乎不是 CSV 格式化的。假设它如上所述,您可以使用Split()CHR(13)转换为字符串数组,然后遍历该数组,删除/仅选择有效项。

PS :在每一行的末尾仍然会有CHR(10),因此在处理数组之前也要删除它们

有关VB6 http://www.thevbprogrammer.com/classic_vbtutorials.asp

的更多帮助

答案 1 :(得分:1)

循环遍历数组并检查每个项目是否满足您正在寻找的条件

例如:

'1 form with:
'  1 command button: name=Command1
Option Explicit

Private mstrArray() As String

Private Sub Command1_Click()
  Dim lngFoundIndex As Long
  Dim strFind As String
  strFind = "hre"
  lngFoundIndex = FindValIndex(strFind)
  MsgBox strFind & " found at index " & CStr(lngFoundIndex), vbInformation, "Result"
End Sub

Private Function FindValIndex(strVal As String) As Long
  Dim lngIndex As Long
  'set return value in case nothing is found
  FindValIndex = -1
  'loop through all items of the array
  For lngIndex = 0 To UBound(mstrArray)
    If InStr(mstrArray(lngIndex), strVal) > 0 Then
      'set return value to found index
      FindValIndex = lngIndex
      Exit For
    End If
  Next lngIndex
End Function

Private Sub Form_Load()
  'fill the array with some example data
  mstrArray = Split("one,two,three,four", ",")
End Sub

上面的代码查找与您要查找的字符串完全相同的项目。 如果您正在寻找包含您正在寻找的字符串的项目,那么您应该看一下Instr()函数

<强> [编辑]

我将上面的代码更改为使用Instr()