2014-04-28 7 views
0

the two ArraysVB6과 어떻게 일치시킬 수 있습니까? 내 프로그램에서는 먼저 행당 CSV file을 구문 분석 한 다음 첫 번째 행의 첫 번째 행을 저장 한 다음 두 번째 행을 두 번째 배열에 저장합니다. 다음으로해야 할 일은 배열의 특정 항목의 차이를 얻는 것입니다. 당신은 두 개의 배열을 발견으로이 VB2에서 두 개의 배열을 일치시키는 방법

입니다

내 첫 번째 배열

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 

입니다 :

예를 들어

가 :

내 문제는 이것이다 길이가 다르면 일치하면

0입니다.
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 

처럼해야 내가 차이를 얻을 때 그것이이

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 
처럼 될 것입니다 수 있다면 그것은이

0 
0 
22 
0 
0 
0 

같이해야한다

를 사용하여 어떻게이 작업을 수행 할 수 있습니까??

+1

샘플 출력이 "사용자 요구 사항"으로 충분하지 않습니다. 출력이 정의되지 않은 경우가 더 많습니다. 출력물이 될 수있는 아이디어에 대해서는 [Levenshtein distance] (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 매우 비슷 기능을 제공하는 경우 모든 값의 종류와 누락 된 항목에 아래의 코드와 주변

플레이보고, 출력은 다르다 그 첫 번째 배열에서 누락되는 경우 CombineArray는 제 2 어레이로부터의 값을 대체하면서 그 CompareArray 누락 값 0이 반환

[EDIT]

배열의 값에 구조 나 논리적 순서가 있습니까?

  • 첫 번째 배열은 다음과 같습니다 : 1,2,4,5
  • 두 번째 배열입니다 : 1,2,3,5
  • 예는 다음과 같은 경우에 당신은 할 결합 배열에 무엇을 원하는가

    • 옵션 A : 1,2,4,3,5
    • 옵션 결과

    어떤 당신이 (왜) 결합 배열이되고 싶어 515,

  • 옵션 B : 1,2,3,4,5

또는 가능한 실제 값 :

  • 최초 배열은 : 10,27,13,12
  • 2 어레이는 : 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 (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

관련 문제