2014-04-28 46 views
0

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

我的问题是这样的:

例如:

这是我的第一个数组

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 

如何使用来实现此目的?

+1

您的示例输出不足以满足“用户需求”。输出未定义的边缘情况更多。查看[Levenshtein距离](http://en.wikipedia.org/wiki/Levenshtein_distance)以获得更多想法,了解输出结果。 – wqw

回答

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第二阵列如果它是在第一阵列中丢失将该值

[编辑]

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

例如:你想怎么组合阵列是如果:

  • 第一阵列: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,4 5,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,以便您可以确定订单以及哪些项目是错误的?

+0

谢谢@Hrqls ..你的样本答案让我感到惊讶。我遇到了另一个问题,如果只有数组intFirst(0),intFirst(1),intFirst(2)和intSecond(0),intSecond(1),intSecond(2)有一个计算并且intFirst(3) ,intSecond(3),intSecond(4),intsecond(5)没有计算并且只获取intSecond的值?怎么做? – bebebe

+0

将两个数组写在纸上,并决定你想要的结果是什么。如果你知道你想获得什么,那么它可以转换成代码..也许通过运行相同的例程两次..尝试上面的代码与intFirst()和intSecond()的值的各种示例,看看会发生什么 – Hrqls

+0

好的,谢谢你... – bebebe