我有一个保存在arraylist.txt文件中的列表。
A1 A2 A3 A4
我需要读取文本文件并将其作为数组。
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
然后我有另一个需要比较的数组:
A2 A3 A4 A5 A1
我需要比较两个数组并在有差异时提示用户。
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。
答案 0 :(得分:2)
如下使用ReDim
Statement;请注意,您需要使用带有空括号的Dim
statement将动态数组声明为Dim arrServiceList()
。声明动态数组后,使用ReDim
循环中的Do Until
语句来定义数组中元素的数量:
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
UBound()
函数返回数组指示维度的最大可用下标。因此,从0
迭代到{{1} } UBound(someArr)
数组(外部迭代)。以下代码段应该给出正确的结果。它迭代&#34;更大&#34;数组优先(外部迭代):
arrServiceList
<强>输出强>
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
编辑#2 :通用解决方案
==> cscript //nologo D:\VB_scripts\SO\q44533806.vbs
q44533806.vbs
A5 not found
==>
<强>输出强>
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