如何將項目添加到VBScript中的現有數組中?將項目添加到VBScript中的數組
是否有一個VBScript等效於JavaScript中的推送功能?
即
myArray的具有三個項目時,「蘋果」,「桔子」,和「香蕉」,我想「西瓜」添加到數組的末尾。
如何將項目添加到VBScript中的現有數組中?將項目添加到VBScript中的數組
是否有一個VBScript等效於JavaScript中的推送功能?
即
myArray的具有三個項目時,「蘋果」,「桔子」,和「香蕉」,我想「西瓜」添加到數組的末尾。
數組在VBScript中不是很動態。你將不得不使用ReDim Preserve語句將現有陣列成長,因此它可以容納額外的項目:
ReDim Preserve yourArray(UBound(yourArray) + 1)
yourArray(UBound(yourArray)) = "Watermelons"
有幾個方面,不包括自定義COM或ActiveX對象
隨着下面的代碼,我發現REDIM保留是最快低於54000,字典是從最快到54000 690000和Array List是上述690000.最快我傾向於使用由於排序和數組轉換而推送的ArrayList。
user326639提供了FastArray,它幾乎是最快的。
字典對於搜索值和返回索引(即字段名稱),或者用於分組和聚合(直方圖,組和連接字符串,組和推子數組)是很有用的。在鍵上分組時,請將CompareMode設置爲/ sensitivity中的大小寫,並在「添加」之前檢查「exists」屬性。
Redim不會爲一個數組節省很多時間,但它對數組字典很有用。
'pushtest.vbs
imax = 10000
value = "Testvalue"
s = imax & " of """ & value & """"
t0 = timer 'ArrayList Method
Set o = CreateObject("System.Collections.ArrayList")
For i = 0 To imax
o.Add value
Next
s = s & "[AList " & FormatNumber(timer - t0, 3, -1) & "]"
Set o = Nothing
t0 = timer 'ReDim Preserve Method
a = array()
For i = 0 To imax
ReDim Preserve a(UBound(a) + 1)
a(UBound(a)) = value
Next
s = s & "[ReDim " & FormatNumber(timer - t0, 3, -1) & "]"
Set a = Nothing
t0 = timer 'Dictionary Method
Set o = CreateObject("Scripting.Dictionary")
For i = 0 To imax
o.Add i, value
Next
s = s & "[Dictionary " & FormatNumber(timer - t0, 3, -1) & "]"
Set o = Nothing
t0 = timer 'Standard array
Redim a(imax)
For i = 0 To imax
a(i) = value
Next
s = s & "[Array " & FormatNumber(timer - t0, 3, -1) & "]" & vbCRLF
Set a = Nothing
t0 = timer 'Fast array
a = array()
For i = 0 To imax
ub = UBound(a)
If i>ub Then ReDim Preserve a(Int((ub+10)*1.1))
a(i) = value
Next
ReDim Preserve a(i-1)
s = s & "[FastArr " & FormatNumber(timer - t0, 3, -1) & "]"
Set a = Nothing
MsgBox s
' 10000 of "Testvalue" [ArrayList 0.156][Redim 0.016][Dictionary 0.031][Array 0.016][FastArr 0.016]
' 54000 of "Testvalue" [ArrayList 0.734][Redim 0.672][Dictionary 0.203][Array 0.063][FastArr 0.109]
' 240000 of "Testvalue" [ArrayList 3.172][Redim 5.891][Dictionary 1.453][Array 0.203][FastArr 0.484]
' 690000 of "Testvalue" [ArrayList 9.078][Redim 44.785][Dictionary 8.750][Array 0.609][FastArr 1.406]
'1000000 of "Testvalue" [ArrayList 13.191][Redim 92.863][Dictionary 18.047][Array 0.859][FastArr 2.031]
我可以添加這段代碼:t0 = timer'Fast array a = array() For i = 0 to imax ub = UBound(a) If i> ub Then ReDim Preserved a(Int( (i-1) a(i)=值 Next ReDim保留一個(i-1) s = s&「[FastArr」&FormatNumber(timer - t0,3,-1)&「] 「 Set a = Nothing – mgr326639 2012-08-15 20:42:08
謝謝!我從來不會想到那個 – Will 2012-11-03 16:35:13
@ mgr326639最後的'ReDim保留一個(i-1)'是怎麼回事? – sirdank 2015-07-08 20:09:59
稍有變化的FastArray從上面:
'pushtest.vbs
imax = 10000000
value = "Testvalue"
s = imax & " of """ & value & """"
t0 = timer 'Fast array
a = array()
ub = UBound(a)
For i = 0 To imax
If i>ub Then
ReDim Preserve a(Int((ub+10)*1.1))
ub = UBound(a)
End If
a(i) = value
Next
ReDim Preserve a(i-1)
s = s & "[FastArr " & FormatNumber(timer - t0, 3, -1) & "]"
MsgBox s
有在的每個週期檢查UBound(a)
沒有點,如果我們確切地知道它的變化。
,以便它檢查不UBound(a)
只是爲開始,然後每次只在ReDim
被稱爲
在我的電腦老方法了7.52秒爲1000萬的IMAX之前,我已經改變了它。
新方法花了5。還有一千萬分之一的imax,這意味着性能提高超過20%(對於千萬次嘗試,顯然這個百分比與嘗試次數有直接關係)
這種情況有點遲,但無論如何,它也是有點棘手
dim arrr
arr= array ("Apples", "Oranges", "Bananas")
dim temp_var
temp_var = join (arr , "||") ' some character which will not occur is regular strings
if len(temp_var) > 0 then
temp_var = temp_var&"||Watermelons"
end if
arr = split(temp_var , "||") ' here you got new elemet in array '
for each x in arr
response.write(x & "<br />")
next'
審查,並告訴我,如果這可以工作 或最初保存在串,後來分裂的所有數據數組
不是答案或者爲什麼「tricky 「不好:
>> a = Array(1)
>> a = Split(Join(a, "||") & "||2", "||")
>> WScript.Echo a(0) + a(1)
>>
12
爲了您的複製和粘貼緩解
' add item to array
Function AddItem(arr, val)
ReDim Preserve arr(UBound(arr) + 1)
arr(UBound(arr)) = val
AddItem = arr
End Function
用於幫助使
a = Array()
a = AddItem(a, 5)
a = AddItem(a, "foo")
感謝狀! – Choy 2011-01-05 14:56:09
請注意,每次使用Redim Preserve時都會複製整個陣列。換句話說,它在大O符號中具有n ** 2的複雜性。 – mgr326639 2012-08-15 21:00:13
@user,複製整個數組具有線性複雜度('O(n)'),而不是二次方('O(n²)')。 – 2012-08-15 21:51:17