Question

How can I match the two Arrays using VB6?Take note: THIS IS DYNAMIC ARRAY. In my program I will parse first the CSV file per line then save the first line in the first Array then the second line will be save in the second array. The next thing that I need to do is to get the difference of the specific items in Arrays.

My problem is this:

For example:

This is my first Array

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

This is my second Array

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

As you notice the two Array have a different length and if you match it this is the structure

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

but it should be like this

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

And when I get the difference It should be like this

0
0
22
0
0
0

If it is possible it will become like this

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

How can I achieve this using VB6?

Was it helpful?

Solution

the project below gives the output that you describe but there are some remarks:

  • is the second array always the complete array with all values?
  • is the first array always smaller and missing some values?
  • does the first array just miss values, or can there also be values which are different from the second array?
  • can the second array also miss values while the first array also misses values on different indices?
  • if the second array is always the complete one, why don't you just use the second array?

play around with the code below with all kinds of values and missing items to see if it still gives the desired results

'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

The functions CompareArray and CombineArray are quite alike, the output is just different in that CompareArray returns a 0 for missing values, while CombineArray substitutes the value from the second array if it is missing in the first array

[EDIT]

is there any structure or logical order in the values in the arrays?

example: what do you want to combined array to be if:

  • First array is : 1,2,4,5
  • Second array is : 1,2,3,5

Which of the optional results do you want the combined array to be (and why) :

  • Option A : 1,2,4,3,5
  • Option B : 1,2,3,4,5

Or with possible real values :

  • First array is : 10,27,13,12
  • Second array is : 10,27,45,12

Optional results :

  • Option A : 10,27,13,45,12
  • Option B : 10,27,45,13,12

Can there be more than 1 subsequent holes ? For example 1,2,3,6 (missing 4 and 5)

Can there be values which are the same at different array items in the same array ? For example 12,27,31,31,43,58 or 12,27,31,43,31,58

What causes the missing values ? Can you add an ID to the values, so that you can determine the order and which items are missings ?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top