在 Vbscript 中比较数组的函数

Function to compare arrays in Vbscript

我正在尝试在 vbscript 中为 UFT 编写一个函数,它比较两个数组并确保两个数组中的所有值都完全匹配。 但是,值的顺序可能不一致。

 Function CompareArrays(array1, array2)

   Dim i,j
   Dim arrkeys1,arrKeys2
   arrkeys1 = array1.keys
   arrkeys2 = array2.keys

  'Do they have the same number of items?
   If array1.Count <> array2.Count Then
    CompareArrays = False
    Exit Function
   End If

  'Compare keys and values

  For i = 0 To UBound(arrKeys2)

      If Not array2.Exists(arrKeys1(i)) Then
     'array1 has a key which array2 doesn't have  
      CompareArrays = False
      Exit Function         
      End If

        For j  = 0 To Ubound(arrkeys1)

          If array1(i) <> array2(j)  Then
            print "Value"& array1(i) " not found in current position" 
            CompareArrays = False
            Else 
            print "Value Found"
            CompareArrays =True
          End If
        Next'j 
  Next 'i 

  End Function

我认为这个脚本的逻辑某处可能有问题。 而且我根本没有输出。 该函数应该只是 return CompareArrays 是 True 或 False。 要比较的数组示例:

dim Array1
set Array1=CreateObject("Scripting.Dictionary")
Array1.Add "Tom", "20"
Array1.Add "Rob", "20"
Array1.Add "Harry", "40"

dim Array2
set Array2=CreateObject("Scripting.Dictionary")
Array2.Add "Rob", "20"
Array2.Add "Tom", "20"
Array2.Add "Harry", "40"

如有任何帮助,我们将不胜感激。

你应该在比较之前尝试对两个数组进行排序,我没有 visual basic 编辑器,但这可能有用:

Imports System
Imports Microsoft.VisualBasic

Public Module Module1

    Public Sub Main()
        dim a1 as Array = {1,2,3}
        dim a2 as Array = {3,2,1}
        dim result = CompareArrays(a1,a2)
        Console.WriteLine("comparison = " & result)

        dim a3 as Array ={"a","b","c"}
        dim a4 as Array = {"b","c","h"}

        result = CompareArrays(a3,a4)
        Console.WriteLine("comparison = " & result)

    End Sub

    Function CompareArrays(array1, array2) as Boolean
        Console.WriteLine(array1)

        If array1.Length <> array2.Length Then
            return false
        End If
       Array.Sort(array1)
       Array.Sort(array2)

        For i as integer = 0 To UBound(array1)
           if(array1(i) <> array2(i))
            return false
           End If
        Next
    return true
    End Function


End Module

output:
comparison = True
comparison = False

我找到了一个 vb 在线编译器,这是示例: https://dotnetfiddle.net/uI1Mp4

在 VBScript 中,Array 不是具有 Keys/Exists/Count 方法的对象。

我认为您实际上是在尝试比较存储键和值的 2 个字典对象。

下面的脚本将比较字典。

 Function CompareArrays(array1, array2)

        'Do they have the same number of items?
        If array1.Count <> array2.Count Then
            CompareArrays = False
            Exit Function
        End If

        'Compare keys and values
        For Each Key in array1.Keys

                'Check Keys
                If NOT(array2.Exists(Key)) Then
                    CompareArrays = False
                    Exit Function
                End If

                'Check Value
                If array1.Item(Key) <> array2.Item(Key) Then
                    CompareArrays = False
                    Exit Function
                End If
        Next

        CompareArrays = True

  End Function