The vbs code below writes a word count (as integer) for each line of string in a document. How do I create a dynamic array (of each line's word count) and sum up the values?
下面的vbs代码为文档中的每一行字符串写入字数(作为整数)。如何创建动态数组(每行的字数)并总结值?
Function AddLineNum()
Dim nLine, sLine, nCount, nLen
nCount = 0
Do Until oInStream.AtEndOfStream
nLine = oInStream.Line
sLine = oInStream.ReadLine
'working word count for each line
nLen = len(sLine) - len(Replace (sLine, " ", "")) +1
oOutStream.WriteLine nLen
nCount = nCount + 1
Loop
AddLineNum = nCount
End Function
2 个解决方案
#1
2
Using standard VBScript arrays you could do this:
使用标准VBScript数组,您可以这样做:
totalCount = 0
arr = Array()
Do Until oInStream.AtEndOfStream
'...
wordCount = UBound(Split(sLine)) + 1
ReDim Preserve arr(UBound(arr)+1)
arr(UBound(arr)) = wordCount
totalCount = totalCount + wordCount
nCount = nCount + 1
Loop
Note that ReDim Preserve
copies the entire array to a new array, so this won't perform well with large arrays.
请注意,ReDim Preserve会将整个阵列复制到一个新阵列,因此对于大型阵列而言,这不会很好。
An alternative would be using the ArrayList
class (requires .NET):
另一种方法是使用ArrayList类(需要.NET):
totalCount = 0
Set arr = CreateObject("System.Collections.ArrayList")
Do Until oInStream.AtEndOfStream
'...
wordCount = UBound(Split(sLine)) + 1
arr.Add wordCount
totalCount = totalCount + wordCount
nCount = nCount + 1
Loop
#2
2
In addition to Ansgar's proposals (ReDim array, ArrayList), you could use a Dictionary:
除了Ansgar的提议(ReDim数组,ArrayList)之外,您还可以使用Dictionary:
cscript wcvbs.vbs
0 217 words according to '\w+' regexp
1 216 words according to wc.bat (perl)
2 319 words according to Len Diff
Dirty details:
* 2 2 "Option Explicit"
* 3 1 ""
* 4 11 "Dim oFS : Set oFS = CreateObject("Scripting.FileSystemObject")"
* 5 1 ""
* 6 8 "Dim reWrd : Set reWrd = New RegExp"
* 7 3 "reWrd.Global = True"
* 8 3 "reWrd.Pattern = "\w+""
* 9 3 "WScript.Echo "0" _"
* 10 5 " , reWrd.Execute(oFS.OpenTextFile(WScript.ScriptFullName).ReadAll()).Count _"
* 11 8 " , "words according to '\w+' regexp""
* 12 1 ""
* 13 11 "Dim oWS : Set oWS = CreateObject("WScript.Shell")"
* 14 10 "Dim oNWS : Set oNWS = New cNWS"
* 15 3 "WScript.Echo "1" _"
* 16 10 " , Split(oNWS.clean(oWS.Exec("wc.bat """ & WScript.ScriptFullName & """").StdOut.ReadAll()))(2) _"
* 17 8 " , "words according to wc.bat (perl)""
* 18 1 ""
* 19 7 "Dim dicWIL : Set dicWIL = CreateObject("Scripting.Dictionary")"
* 20 11 "Dim tsIn : Set tsIn = oFS.OpenTextFile(WScript.ScriptFullName)"
* 21 14 "Dim nSum : nSum = 0"
* 22 2 "Dim nLine"
* 23 3 "Do Until tsIn.AtEndOfStream"
* 24 9 " Dim sLine : sLine = tsIn.ReadLine()"
* 25 14 " dicWIL(tsIn.Line) = Array(Len(sLine) - Len(Replace(sLine, " ", "")) + 1, sLine)"
* 26 8 " nSum = nSum + dicWIL(tsIn.Line)(0)"
* 27 1 "Loop"
* 28 1 "tsIn.Close"
* 29 3 "WScript.Echo "2" _"
* 30 5 " , nSum _"
* 31 8 " , "words according to Len Diff""
* 32 3 "WScript.Echo "Dirty details:""
* 33 5 "For Each nLine In dicWIL.Keys"
* 34 9 " WScript.Echo "*", nLine, dicWIL(nLine)(0), qq(dicWIL(nLine)(1))"
* 35 1 "Next"
* 36 1 ""
* 37 2 "WScript.Quit 0"
* 38 1 ""
* 39 13 "Function qq(s) : qq = """" & s & """" : End Function"
* 40 1 ""
* 41 7 "Class cNWS ' normalize (trim, reduce) whitespace"
* 42 4 " Private m_reTrim"
* 43 4 " Private m_reReduce"
* 44 5 " Private Sub Class_Initialize()"
* 45 15 " Set m_reTrim = New RegExp"
* 46 10 " m_reTrim.Global = True"
* 47 9 " m_reTrim.Pattern = "^\w+|\s+$""
* 48 13 " Set m_reReduce = New RegExp"
* 49 8 " m_reReduce.Global = True"
* 50 7 " m_reReduce.Pattern = "\s+""
* 51 4 " End Sub"
* 52 5 " Public Function clean(s)"
* 53 10 " clean = m_reReduce.Replace(m_reTrim.Replace(s, ""), " ")"
* 54 4 " End Function"
* 55 3 " End Class"
#1
2
Using standard VBScript arrays you could do this:
使用标准VBScript数组,您可以这样做:
totalCount = 0
arr = Array()
Do Until oInStream.AtEndOfStream
'...
wordCount = UBound(Split(sLine)) + 1
ReDim Preserve arr(UBound(arr)+1)
arr(UBound(arr)) = wordCount
totalCount = totalCount + wordCount
nCount = nCount + 1
Loop
Note that ReDim Preserve
copies the entire array to a new array, so this won't perform well with large arrays.
请注意,ReDim Preserve会将整个阵列复制到一个新阵列,因此对于大型阵列而言,这不会很好。
An alternative would be using the ArrayList
class (requires .NET):
另一种方法是使用ArrayList类(需要.NET):
totalCount = 0
Set arr = CreateObject("System.Collections.ArrayList")
Do Until oInStream.AtEndOfStream
'...
wordCount = UBound(Split(sLine)) + 1
arr.Add wordCount
totalCount = totalCount + wordCount
nCount = nCount + 1
Loop
#2
2
In addition to Ansgar's proposals (ReDim array, ArrayList), you could use a Dictionary:
除了Ansgar的提议(ReDim数组,ArrayList)之外,您还可以使用Dictionary:
cscript wcvbs.vbs
0 217 words according to '\w+' regexp
1 216 words according to wc.bat (perl)
2 319 words according to Len Diff
Dirty details:
* 2 2 "Option Explicit"
* 3 1 ""
* 4 11 "Dim oFS : Set oFS = CreateObject("Scripting.FileSystemObject")"
* 5 1 ""
* 6 8 "Dim reWrd : Set reWrd = New RegExp"
* 7 3 "reWrd.Global = True"
* 8 3 "reWrd.Pattern = "\w+""
* 9 3 "WScript.Echo "0" _"
* 10 5 " , reWrd.Execute(oFS.OpenTextFile(WScript.ScriptFullName).ReadAll()).Count _"
* 11 8 " , "words according to '\w+' regexp""
* 12 1 ""
* 13 11 "Dim oWS : Set oWS = CreateObject("WScript.Shell")"
* 14 10 "Dim oNWS : Set oNWS = New cNWS"
* 15 3 "WScript.Echo "1" _"
* 16 10 " , Split(oNWS.clean(oWS.Exec("wc.bat """ & WScript.ScriptFullName & """").StdOut.ReadAll()))(2) _"
* 17 8 " , "words according to wc.bat (perl)""
* 18 1 ""
* 19 7 "Dim dicWIL : Set dicWIL = CreateObject("Scripting.Dictionary")"
* 20 11 "Dim tsIn : Set tsIn = oFS.OpenTextFile(WScript.ScriptFullName)"
* 21 14 "Dim nSum : nSum = 0"
* 22 2 "Dim nLine"
* 23 3 "Do Until tsIn.AtEndOfStream"
* 24 9 " Dim sLine : sLine = tsIn.ReadLine()"
* 25 14 " dicWIL(tsIn.Line) = Array(Len(sLine) - Len(Replace(sLine, " ", "")) + 1, sLine)"
* 26 8 " nSum = nSum + dicWIL(tsIn.Line)(0)"
* 27 1 "Loop"
* 28 1 "tsIn.Close"
* 29 3 "WScript.Echo "2" _"
* 30 5 " , nSum _"
* 31 8 " , "words according to Len Diff""
* 32 3 "WScript.Echo "Dirty details:""
* 33 5 "For Each nLine In dicWIL.Keys"
* 34 9 " WScript.Echo "*", nLine, dicWIL(nLine)(0), qq(dicWIL(nLine)(1))"
* 35 1 "Next"
* 36 1 ""
* 37 2 "WScript.Quit 0"
* 38 1 ""
* 39 13 "Function qq(s) : qq = """" & s & """" : End Function"
* 40 1 ""
* 41 7 "Class cNWS ' normalize (trim, reduce) whitespace"
* 42 4 " Private m_reTrim"
* 43 4 " Private m_reReduce"
* 44 5 " Private Sub Class_Initialize()"
* 45 15 " Set m_reTrim = New RegExp"
* 46 10 " m_reTrim.Global = True"
* 47 9 " m_reTrim.Pattern = "^\w+|\s+$""
* 48 13 " Set m_reReduce = New RegExp"
* 49 8 " m_reReduce.Global = True"
* 50 7 " m_reReduce.Pattern = "\s+""
* 51 4 " End Sub"
* 52 5 " Public Function clean(s)"
* 53 10 " clean = m_reReduce.Replace(m_reTrim.Replace(s, ""), " ")"
* 54 4 " End Function"
* 55 3 " End Class"