在VBScript中将项添加到数组

Cho*_*hoy 43 arrays vbscript

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

是否有一个VBScript等效于Javascript中的push函数?

myArray有三个项目,"苹果","橙子"和"香蕉",我想在阵列的末尾添加"西瓜".

Fré*_*idi 62

数组在VBScript中不是很动态.您必须使用ReDim Preserve语句来扩展现有数组,以便它可以容纳额外的项目:

ReDim Preserve yourArray(UBound(yourArray) + 1)
yourArray(UBound(yourArray)) = "Watermelons"
Run Code Online (Sandbox Code Playgroud)

  • 请注意,"Redim Preserve"会在每次使用时复制整个阵列.换句话说,它在big-O表示法中具有n**2的复杂性. (8认同)
  • @用户,复制整个数组具有线性复杂度(`O(n)`),而不是二次复杂度(`O(n²)`)。 (2认同)
  • 当然,制作一个副本是线性的.我在这里的意思是以这种方式构建阵列.使用Redim Preserve填充100k阵列与200k阵列绝对需要4倍,而不是2倍.这不仅仅是理论,你可以自己尝试,你会看到.我通常所做的(并且易于实现但意味着巨大的性能提升)是每当索引要高于上限时将上限增加1.1倍. (2认同)
  • 在给出ReDim Preserve语句时(在用Dim初始化之后),我有一个类型不匹配:'UBound'... (2认同)

Wil*_*ill 10

有几种方法,不包括自定义COM或ActiveX对象

  1. ReDim Preserve
  2. 字典对象,可以有字符串键并搜索它们
  3. ArrayList .Net Framework类,有许多方法,包括:sort(forward,reverse,custom),insert,remove,binarysearch,equals,toArray和toString

使用下面的代码,我发现Redim Preserve最快速度低于54000,Dictionary最快速度从54000到690000,而Array List最快速度高于690000.由于排序和数组转换,我倾向于使用ArrayList进行推送.

user326639提供了FastArray,这是最快的.

字典对于搜索值和返回索引(即字段名称),或用于分组和聚合(直方图,组和添加,组和连接字符串,组和推送子数组)非常有用.在对键进行分组时,为case in/sensitivity设置CompareMode,并在"add"-ing之前检查"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]
Run Code Online (Sandbox Code Playgroud)

  • 我建议你添加这段代码: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)=值下一个ReDim保留a(i-1)s = s&"[FastArr"&FormatNumber(timer-t0,3,-1)&"]"设置a = Nothing (3认同)

Cha*_*ton 9

为您的复制和粘贴轻松

' add item to array
Function AddItem(arr, val)
    ReDim Preserve arr(UBound(arr) + 1)
    arr(UBound(arr)) = val
    AddItem = arr
End Function
Run Code Online (Sandbox Code Playgroud)

像这样使用

a = Array()
a = AddItem(a, 5)
a = AddItem(a, "foo")
Run Code Online (Sandbox Code Playgroud)