2013-04-07 81 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

相关问题