1

下面的 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
4

2 回答 2

2

使用标准 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

请注意,ReDim Preserve将整个数组复制到一个新数组,因此这对于大型数组来说效果不佳。

另一种方法是使用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
于 2013-04-07T19:40:53.210 回答
2

除了 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"
于 2013-04-07T21:57:05.920 回答