How to match two Arrays in VB6



如何使用VB6匹配the two Arrays?请注意:这是动态阵列。在我的程序中,我将首先解析每行的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 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则在第一个数组中丢失第二个数组的值时替换该值

[EDIT]

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

示例:如果:,您希望组合阵列是什么

  • 第一个数组为: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,12

是否可以有1个以上的后续孔?例如1,2,3,6(缺少4和5)

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

是什么原因导致缺少值?你能在值中添加一个ID,这样你就可以确定订单和哪些项目是缺失的吗?

最新更新