除了 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"