将项目添加到VBScript中的数组

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

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

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

VBScript中的数组不是很dynamic的。 您将不得不使用ReDim Preserve语句来增长现有的数组,以便它可以容纳一个额外的项目:

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

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

  1. ReDim保存
  2. 字典对象,可以有string键和search它们
  3. ArrayList .Net框架类,它有许多方法,包括:sorting(正向,反向,自定义),插入,删除,binarysearch,等于,toArray和toString

使用下面的代码,我发现Redim Preserve在54000以下是最快的,Dictionary是54000到690000中最快的,而Array List是690000以上的最快的。我倾向于使用ArrayList来推送,因为sorting和数组转换。

user326639提供了FastArray,这几乎是最快的。

字典可用于search值并返回索引(即字段名称),或者用于分组和聚合(直方图,组和连接string,组和连接子数组)。 在键上分​​组时,将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] 

为了您的复制和粘贴的方便

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

从上面稍微更改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)就在for开始之前,然后每次调用ReDim

在我的电脑上,老方法花费了7.52秒,用了一千万的imax。

这个新方法花费了5.29秒,也就是1000万的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' 

审查并告诉我,如果这可以工作,或者最初您将所有数据保存在string中,然后拆分为数组

不是答案为什么' 棘手 '是不好的

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