larrey
larrey

Reputation: 73

comparing two arrays with different elements and location of elements

I have a list that is saved in arraylist.txt file.

A1
A2
A3
A4

I need to read the text file and make it as an array.

Const ForReading = 1
Dim arrServiceList
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("D:\TestStatus\arraylist.txt", ForReading)

Do Until objTextFile.AtEndOfStream
  strNextLine = objTextFile.Readline
  arrServiceList = Split(strNextLine , "")
  Wscript.Echo "Server name: " & arrServiceList(0)
  For i = 1 To UBound(arrServiceList)
    WScript.Echo "Service: " & arrServiceList(i)
  Next
Loop

Then I have another array that need to compare:

A2
A3
A4
A5
A1

I need to compare both arrays and prompt the user if there are differences.

Dim i 'As Integer
Dim j 'As Integer
Dim isFound 'As Boolean

For i = 0 To UBound(tempArr) - 1
  isFound = False
  For j = 0 To UBound(arrServiceList) - 1
    If tempArr(i) = arrServiceList(j) Then
      isFound = True
    End If
  Next 'j
  If Not isFound Then
    MsgBox  tempArr & "not found"
  End If
Next 'i

But the output all not found. The result should be A5 not found.

Upvotes: 0

Views: 771

Answers (1)

JosefZ
JosefZ

Reputation: 30113

I need to read the text file and make it as an array.

Utilise ReDim Statement as follows; note that you need to use the Dim statement with empty parentheses to declare a dynamic array as Dim arrServiceList(). After declaring a dynamic array, use the ReDim statement within Do Until loop to define the number of elements in the array:

Const ForReading = 1
Dim arrServiceList(), objFSO, objTextFile, strNextLine, i
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("D:\TestStatus\arraylist.txt", ForReading)

i = 0
Do Until objTextFile.AtEndOfStream
  strNextLine = objTextFile.Readline
  ' omit empty lines
  If Trim( strNextLine) <> "" Then
    ReDim Preserve arrServiceList( i)
    arrServiceList( i) = Trim( strNextLine)
    i = i + 1
  End If
Loop
objTextFile.Close

I need to compare both arrays.

  1. UBound() function returns the largest available subscript for the indicated dimension of an array. Hence, iterate from 0 to UBound(someArr)
  2. Iterate arrServiceList array first (outer iteration).

The following code snippet should give proper result. It iterates "bigger" array first (outer iteration):

Option Explicit
On Error GoTo 0
Dim strResult: strResult = Wscript.ScriptName
Dim tempArr
tempArr = Split("A1 A2 A3 A4") ' default delimiter = space
Dim arrServiceList
arrServiceList = Split("A2 A3 A4 A5 A1") ' default delimiter = space

Dim i 'As Integer
Dim j 'As Integer
Dim ArrBig, ArrLes
If UBound(tempArr) > UBound(arrServiceList) Then
  ArrBig = tempArr
  ArrLes = arrServiceList
Else
  ArrBig = arrServiceList
  ArrLes = tempArr
End If
Dim isFound 'As Boolean
For i = 0 To UBound(arrBig)     ''' - 1
    isFound = False
    For j = 0 To UBound(arrLes) ''' - 1
        If arrBig(i) = arrLes(j) Then
            isFound = True
        End If
    Next 'j
    If Not isFound Then
        strResult = strResult & vbNewLine & arrBig(i) & " not found"
    End If
Next 'i

Wscript.Echo strResult

Output

==> cscript //nologo D:\VB_scripts\SO\q44533806.vbs
q44533806.vbs
A5 not found

==>

Edit #2: universal solution

Option Explicit
On Error GoTo 0
Dim strResult: strResult = Wscript.ScriptName
Dim tempArr
tempArr = Split("A1 A9 A3 A4 A6") ' default delimiter = space
Dim arrServiceList
arrServiceList = Split("A2 A8 A4 A5 A1") ' default delimiter = space

CompareArrays "arrServiceList", tempArr       , arrServiceList
CompareArrays "tempArr"       , arrServiceList, tempArr

Wscript.Echo strResult
Wscript.Quit

Sub CompareArrays( strArrName, ByRef ArrBig,  ByRef ArrLes)
  strResult = strResult & vbNewLine _
            & "items not found in " & strArrName
  Dim i, j, isFound
  For i = 0 To UBound(arrBig)
      isFound = False
      For j = 0 To UBound(arrLes)
          If arrBig(i) = arrLes(j) Then 
              isFound = True
              Exit For          'j'
          End If
      Next 'j
      If Not isFound Then
          strResult = strResult & vbNewLine _
            & arrBig(i) ' & " not found in " & strArrName 
      End If
  Next 'i
End Sub

Output

==> cscript //NOLOGO D:\VB_scripts\SO\q44533806b.vbs
q44533806b.vbs
items not found in arrServiceList
A9
A3
A6
items not found in tempArr
A2
A8
A5

==>

Upvotes: 2

Related Questions