Это не ошибка дизайна . Любой, кто совершает это, делает что-то неправильно.
Есть три случая, когда я вижу, где вы можете столкнуться с этой проблемой:
cache={}
, и вы не должны были бы вызывать функцию с фактическим аргументом вообще. Пример в вопросе может относиться к категории 1 или 3. Нечетно, что он изменяет переданный список и возвращает его; вы должны выбрать тот или другой.
Все, что вам нужно сделать, так как вы читаете массив в обратном направлении (UBound
- LBound
), это выйти из цикла, как только вы найдете искомый текст, а не продолжать его. [113 ]
Sub FileSubTot
Dim arrFileLines()
Dim choice
choice="SUBTOT"
i = 0
'opens txt file and makes each line an element in an array called arrFileLines
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\stuff\etc\etc...", 1)
Do Until objFile.AtEndOfStream
Redim Preserve arrFileLines(i)
arrFileLines(i) = objFile.ReadLine
i = i + 1
Loop
objFile.Close
'iterates through the array looking for the word SUBTOT then grabs the subtotal value and compares to the (TTP)
For i = Ubound(arrFileLines) to LBound(arrFileLines) Step -1
If InStr(arrFileLines(i), choice) <> 0 Then
Log.Message "Found " & choice
Log.Message arrFileLines(i)
Total=Split(arrFileLines(i)," ",-1)
Log.Message "TOTAL TO PAY: €"& Total(1)
Exit For ' This will exit the For Loop once the choice is found
End If
Next
End Sub
Filter
- полезная функция для такого рода вещей
' build a test string
dim a(4), i
for i = 0 to 4
if i mod 2 = 1 then
a(i) = "Subtot " & (i + 1)
else
a(i) = "Something else"
end if
next
WScript.Echo Join(a, vbNewLine)
'reduce the array to a subset that just contains "subtot"
dim b: b = Filter(a, "subtot", true, vbTextCompare)
' use the last item in the array
i = UBound(b)
WScript.Echo b(i)
Выходы
Что-то еще
Подтотон 2
Что-то еще
Подраздел 4
Что-то ещеПодтотон 4
blockquote>