比较具有不同元素和元素位置的两个数组 [英] comparing two arrays with different elements and location of elements

查看:141
本文介绍了比较具有不同元素和元素位置的两个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个保存在arraylist.txt文件中的列表。

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

但是输出全部未找到。
结果应为A5。

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

推荐答案

我需要读取文本文件并将其作为数组。



使用 ReDim 语句,如下所示;请注意,您需要使用 Dim 语句(带空括号)将动态数组声明为 DimarrServiceList()。声明动态数组后,在 Dotil直到$code>循环中使用 ReDim 语句定义数组中的元素数:

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()函数返回数组指示尺寸的最大可用下标因此,从 0 迭代到 UBound(someArr)

  2. 首先迭代 arrServiceList 数组(外部迭代)。

  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

输出

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

==>

编辑#2 :通用解决方案

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

输出

==> 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

==>

这篇关于比较具有不同元素和元素位置的两个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆