如何在VB6中匹配两个数组

时间:2014-04-28 08:28:06

标签: arrays vb6

如何使用the two Arrays匹配VB6?请注意:这是动态阵列。在我的程序中,我将首先解析每行CSV file然后将第一行保存在第一个数组中,然后第二行将保存在第二个数组中。我需要做的下一件事是获取数组中特定项的差异。

我的问题是:

例如:

这是我的第一个数组

MyArray(0) => 10
MyArray(1) => 45
MyArray(2) => 3
MyArray(3) => 0
MyArray(4) => 89

这是我的第二个数组

MysecondArray(0) => 10
MysecondArray(1) => 45
MysecondArray(2) => 22
MysecondArray(3) => 3
MysecondArray(4) => 0
MysecondArray(5) => 89

正如您所注意到的那样,两个数组的长度不同,如果匹配,则为结构

MyArray(0) => 10                   MysecondArray(0) => 10
MyArray(1) => 45                   MysecondArray(1) => 45
MyArray(2) => 3                    MysecondArray(2) => 22
MyArray(3) => 0                    MysecondArray(3) => 3
MyArray(4) => 89                   MysecondArray(4) => 0
                                   MysecondArray(5) => 89

但它应该是这样的

MyArray(0) => 10                   MysecondArray(0) => 10
MyArray(1) => 45                   MysecondArray(1) => 45
                                   MysecondArray(2) => 22
MyArray(2) => 3                    MysecondArray(3) => 3
MyArray(3) => 0                    MysecondArray(4) => 0
MyArray(4) => 89                   MysecondArray(5) => 89

当我得到差异它应该是这样的

0
0
22
0
0
0

如果有可能它会变成这样

MyArray(0) => 10                   MysecondArray(0) => 10
MyArray(1) => 45                   MysecondArray(1) => 45
MyArray(2) => 0                    MysecondArray(2) => 22
MyArray(3) => 3                    MysecondArray(3) => 3
MyArray(4) => 0                    MysecondArray(4) => 0
MyArray(5) => 89                   MysecondArray(5) => 89

如何使用VB6实现此目的?

1 个答案:

答案 0 :(得分:1)

下面的项目给出了您描述的输出,但有一些评论:

  • 是第二个数组总是带有所有值的完整数组吗?
  • 是第一个总是较小且缺少某些值的数组吗?
  • 第一个数组是否会错过值,还是可能存在与第二个数组不同的值?
  • 第二个数组是否也会错过值,而第一个数组也会错过不同索引上的值?
  • 如果第二个数组总是完整的那个,你为什么不只使用第二个数组?

使用下面的代码,使用各种值和缺少的项目来查看它是否仍能提供所需的结果

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

Private Sub Command1_Click()
  Dim intFirst(4) As Integer
  Dim intSecond(5) As Integer
  Dim intDiff() As Integer
  Dim intCombo() As Integer
  'fill first array with example values
  intFirst(0) = 10
  intFirst(1) = 45
  intFirst(2) = 3
  intFirst(3) = 0
  intFirst(4) = 89
  'fill second array with example values
  intSecond(0) = 10
  intSecond(1) = 45
  intSecond(2) = 22
  intSecond(3) = 3
  intSecond(4) = 0
  intSecond(5) = 89
  'compare arrays
  intDiff = CompareArrays(intFirst, intSecond)
  'combine arrays
  intCombo = CombineArrays(intFirst, intSecond)
  'print the arrays
  Print "First"
  PrintArray intFirst
  Print "Second"
  PrintArray intSecond
  Print "Difference"
  PrintArray intDiff
  Print "Combination"
  PrintArray intCombo
End Sub

Private Function CompareArrays(intFirst() As Integer, intSecond() As Integer) As Integer()
  Dim intDiff() As Integer
  Dim intUbound As Integer
  Dim intIndex As Integer
  Dim intSkip As Integer
  intUbound = UBound(intSecond)
  'make sure the second array has the highest ubound
  If UBound(intFirst) > intUbound Then
    'call function with array with highest ubound as second argument
    intDiff = CompareArrays(intSecond, intFirst)
  Else
    'resize intDiff to the same size as intSecond
    ReDim intDiff(intUbound) As Integer
    intSkip = 0
    'compare each item in the arrays
    For intIndex = 0 To intUbound
      If intFirst(intIndex - intSkip) = intSecond(intIndex) Then
        intDiff(intIndex) = 0
      Else
        intDiff(intIndex) = intSecond(intIndex)
        intSkip = intSkip + 1
      End If
    Next intIndex
  End If
  CompareArrays = intDiff
End Function

Private Function CombineArrays(intFirst() As Integer, intSecond() As Integer) As Integer()
  Dim intCombo() As Integer
  Dim intUbound As Integer
  Dim intIndex As Integer
  Dim intSkip As Integer
  intUbound = UBound(intSecond)
  'make sure the second array has the highest ubound
  If UBound(intFirst) > intUbound Then
    'call function with array with highest ubound as second argument
    intCombo = CombineArrays(intSecond, intFirst)
  Else
    'resize intDiff to the same size as intSecond
    ReDim intCombo(intUbound) As Integer
    intSkip = 0
    'compare each item in the arrays
    For intIndex = 0 To intUbound
      If intFirst(intIndex - intSkip) = intSecond(intIndex) Then
        intCombo(intIndex) = intSecond(intIndex)
      Else
        intCombo(intIndex) = intSecond(intIndex)
        intSkip = intSkip + 1
      End If
    Next intIndex
  End If
  CombineArrays = intCombo
End Function

Private Sub PrintArray(intArray() As Integer)
  Dim intIndex As Integer
  For intIndex = 0 To UBound(intArray)
    Print CStr(intArray(intIndex))
  Next intIndex
End Sub

Private Sub Form_Load()
  Height = 7200
End Sub

CompareArray和CombineArray函数非常相似,输出的不同之处在于CompareArray为缺失值返回0,而CombineArray替换第二个数组中的值(如果第一个数组中缺少它)

<强> [编辑]

数组中的值是否有任何结构或逻辑顺序?

示例:您希望将数组组合成if:

  • 第一个数组是:1,2,4,5
  • 第二个数组是:1,2,3,5

您希望组合数组成为哪个可选结果(及其原因):

  • 选项A:1,2,4,3,5
  • 选项B:1,2,3,4,5

或者有可能的真实值:

  • 第一个数组是:10,27,13,12
  • 第二个数组是:10,27,45,12

可选结果:

  • 选项A:10,27,13,45,12
  • 选项B:10,27,45,13,​​12

后续漏洞可以超过1个吗?例如1,2,3,6(缺少4和5)

同一个数组中不同数组项的值是否相同?例如12,27,31,31,43,58或12,27,31,43,31,58

导致缺失值的原因是什么?您可以在值中添加ID,以便确定订单和丢失的项目吗?