2013-04-07 83 views
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 

回答

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 
+0

謝謝爲迴應。我會盡力實現你的代碼。我會發布,如果我能得到它的工作。至於數組大小,我正在處理相對較小的列表,所以它不會是一個問題。 – 2013-04-08 02:09:49

2

除了安斯加爾的建議(使用ReDim陣列,ArrayList中),你可以使用字典:

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" 
+0

啊,是的。我忘記了字典。 – 2013-04-07 22:22:58

+0

這對我來說都是新鮮的!感謝分享。我將使用我可以嵌入現有文本過濾器的任何一段代碼。會讓你知道。 – 2013-04-08 02:14:40

相關問題