I am trying to write a function for UFT in vbscript which compare two arrays and makes sure all the values in the both arrays are an exact match. However, the order of the values could be inconsistent.
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
I think there might be a problem with the logic of this script somewhere. And I am getting no output at all. The function should just return CompareArrays are True or False. Example of arrays to be compared :
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"
Any help would be greatly appreciated.
In VBScript, Array is not an object which has the Keys/Exists/Count method.
I think you are actually trying to compare 2 dictionary objects which stores keys and values.
Below script will compare the dictionary.
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