2011-01-05 58 views
37

如何将项目添加到VBScript中的现有数组中?将项目添加到VBScript中的数组

是否有一个VBScript等效于JavaScript中的推送功能?

myArray的具有三个项目时,“苹果”,“桔子”,和“香蕉”,我想“西瓜”添加到数组的末尾。

回答

55

数组在VBScript中不是很动态。你将不得不使用ReDim Preserve语句将现有阵列成长,因此它可以容纳额外的项目:

ReDim Preserve yourArray(UBound(yourArray) + 1) 
yourArray(UBound(yourArray)) = "Watermelons" 
+0

感谢状! – Choy 2011-01-05 14:56:09

+6

请注意,每次使用Redim Preserve时都会复制整个阵列。换句话说,它在大O符号中具有n ** 2的复杂性。 – mgr326639 2012-08-15 21:00:13

+0

@user,复制整个数组具有线性复杂度('O(n)'),而不是二次方('O(n²)')。 – 2012-08-15 21:51:17

9

有几个方面,不包括自定义COM或ActiveX对象

  1. 使用ReDim保留
  2. Dictionary对象,它可以有字符串键和搜索他们
  3. ArrayList的.NET Framework类,其中有许多方法,包括: 排序(正转,反转,自定义),插入,删除, 的binarySearch,等于,指定者和toString

随着下面的代码,我发现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] 
+3

我可以添加这段代码: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

+0

谢谢!我从来不会想到那个 – Will 2012-11-03 16:35:13

+0

@ mgr326639最后的'ReDim保留一个(i-1)'是怎么回事? – sirdank 2015-07-08 20:09:59

3

稍有变化的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%(对于千万次尝试,显然这个百分比与尝试次数有直接关系)

0

这种情况有点迟,但无论如何,它也是有点棘手

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' 

审查,并告诉我,如果这可以工作 或最初保存在串,后来分裂的所有数据数组

0

不是答案或者为什么“tricky “不好

>> a = Array(1) 
>> a = Split(Join(a, "||") & "||2", "||") 
>> WScript.Echo a(0) + a(1) 
>> 
12 
4

为了您的复制和粘贴缓解

' 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")