How do you add an item to an existing array in VBScript?
如何向VBScript中的现有数组添加项?
Is there a VBScript equivalent to the push function in Javascript?
是否有一个VBScript等价于Javascript中的push函数?
i.e.
即。
myArray has three items, "Apples", "Oranges", and "Bananas" and I want to add "Watermelons" to the end of the array.
myArray有三个项目,“苹果”、“橘子”和“香蕉”,我想在数组的末尾添加“西瓜”。
6 个解决方案
#1
56
Arrays are not very dynamic in VBScript. You'll have to use the ReDim Preserve statement to grow the existing array so it can accommodate an extra item:
数组在VBScript中不是非常动态的。您将不得不使用ReDim保存语句来扩展现有的数组,以便它可以容纳一个额外的项目:
ReDim Preserve yourArray(UBound(yourArray) + 1)
yourArray(UBound(yourArray)) = "Watermelons"
#2
9
There are a few ways, not including a custom COM or ActiveX object
有几种方法,不包括自定义COM或ActiveX对象。
- ReDim Preserve
- ReDim保存
- Dictionary object, which can have string keys and search for them
- Dictionary对象,它可以有字符串键并搜索它们。
- ArrayList .Net Framework Class, which has many methods including: sort (forward, reverse, custom), insert, remove, binarysearch, equals, toArray, and toString
- ArrayList .Net Framework类,它有许多方法,包括:sort(向前、反向、自定义)、插入、删除、binarysearch、equals、toArray和toString。
With the code below, I found Redim Preserve is fastest below 54000, Dictionary is fastest from 54000 to 690000, and Array List is fastest above 690000. I tend to use ArrayList for pushing because of the sorting and array conversion.
通过下面的代码,我发现Redim保存速度最快在54000以下,字典最快从54000到690000,数组列表最快超过690000。由于排序和数组转换,我倾向于使用ArrayList进行推送。
user326639 provided FastArray, which is pretty much the fastest.
user326639提供了FastArray,这是最快的。
Dictionaries are useful for searching for the value and returning the index (i.e. field names), or for grouping and aggregation (histograms, group and add, group and concatenate strings, group and push sub-arrays). When grouping on keys, set CompareMode for case in/sensitivity, and check the "exists" property before "add"-ing.
字典对于搜索值和返回索引(即字段名)、分组和聚合(直方图、组和添加、组和连接字符串、组和push子数组)非常有用。当对键进行分组时,在“添加”之前对“存在”属性进行设置比较,并检查“存在”属性。
Redim wouldn't save much time for one array, but it's useful for a dictionary of arrays.
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
4
For your copy and paste ease
为了您的复制和粘贴方便。
' add item to array
Function AddItem(arr, val)
ReDim Preserve arr(UBound(arr) + 1)
arr(UBound(arr)) = val
AddItem = arr
End Function
Used like so
使用这样的
a = Array()
a = AddItem(a, 5)
a = AddItem(a, "foo")
#4
3
Slight change to the FastArray from above:
从上方轻微的变化到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
There is no point in checking UBound(a)
in every cycle of the for if we know exactly when it changes.
如果我们准确地知道它何时发生变化,那么在每一个周期中检查UBound(a)是没有意义的。
I've changed it so that it checks does UBound(a)
just before the for starts and then only every time the ReDim
is called
我已经更改了它,以便它在开始之前检查UBound(a),然后每次调用ReDim时都进行检查。
On my computer the old method took 7.52 seconds for an imax of 10 millions.
在我的电脑上,这个老方法用了7.52秒的时间进行了1000万的imax放映。
The new method took 5.29 seconds for an imax of also 10 millions, which signifies a performance increase of over 20% (for 10 millions tries, obviously this percentage has a direct relationship to the number of tries)
这个新方法花费了5.29秒的时间,imax也有1000万,这意味着性能增长超过20%(1000万尝试,很明显这个百分比与尝试的数量有直接关系)
#5
0
this some kind of late but anyway and it is also somewhat tricky
这有点晚了,但不管怎样,这也有点棘手。
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'
review and tell me if this can work or initially you save all data in string and later split for array
回顾并告诉我这是否可行,或者最初你将所有数据保存在字符串中,然后将其拆分为数组。
#6
0
Not an answer Or Why 'tricky' is bad:
没有一个答案或者为什么“棘手”是不好的:
>> a = Array(1)
>> a = Split(Join(a, "||") & "||2", "||")
>> WScript.Echo a(0) + a(1)
>>
12
#1
56
Arrays are not very dynamic in VBScript. You'll have to use the ReDim Preserve statement to grow the existing array so it can accommodate an extra item:
数组在VBScript中不是非常动态的。您将不得不使用ReDim保存语句来扩展现有的数组,以便它可以容纳一个额外的项目:
ReDim Preserve yourArray(UBound(yourArray) + 1)
yourArray(UBound(yourArray)) = "Watermelons"
#2
9
There are a few ways, not including a custom COM or ActiveX object
有几种方法,不包括自定义COM或ActiveX对象。
- ReDim Preserve
- ReDim保存
- Dictionary object, which can have string keys and search for them
- Dictionary对象,它可以有字符串键并搜索它们。
- ArrayList .Net Framework Class, which has many methods including: sort (forward, reverse, custom), insert, remove, binarysearch, equals, toArray, and toString
- ArrayList .Net Framework类,它有许多方法,包括:sort(向前、反向、自定义)、插入、删除、binarysearch、equals、toArray和toString。
With the code below, I found Redim Preserve is fastest below 54000, Dictionary is fastest from 54000 to 690000, and Array List is fastest above 690000. I tend to use ArrayList for pushing because of the sorting and array conversion.
通过下面的代码,我发现Redim保存速度最快在54000以下,字典最快从54000到690000,数组列表最快超过690000。由于排序和数组转换,我倾向于使用ArrayList进行推送。
user326639 provided FastArray, which is pretty much the fastest.
user326639提供了FastArray,这是最快的。
Dictionaries are useful for searching for the value and returning the index (i.e. field names), or for grouping and aggregation (histograms, group and add, group and concatenate strings, group and push sub-arrays). When grouping on keys, set CompareMode for case in/sensitivity, and check the "exists" property before "add"-ing.
字典对于搜索值和返回索引(即字段名)、分组和聚合(直方图、组和添加、组和连接字符串、组和push子数组)非常有用。当对键进行分组时,在“添加”之前对“存在”属性进行设置比较,并检查“存在”属性。
Redim wouldn't save much time for one array, but it's useful for a dictionary of arrays.
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
4
For your copy and paste ease
为了您的复制和粘贴方便。
' add item to array
Function AddItem(arr, val)
ReDim Preserve arr(UBound(arr) + 1)
arr(UBound(arr)) = val
AddItem = arr
End Function
Used like so
使用这样的
a = Array()
a = AddItem(a, 5)
a = AddItem(a, "foo")
#4
3
Slight change to the FastArray from above:
从上方轻微的变化到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
There is no point in checking UBound(a)
in every cycle of the for if we know exactly when it changes.
如果我们准确地知道它何时发生变化,那么在每一个周期中检查UBound(a)是没有意义的。
I've changed it so that it checks does UBound(a)
just before the for starts and then only every time the ReDim
is called
我已经更改了它,以便它在开始之前检查UBound(a),然后每次调用ReDim时都进行检查。
On my computer the old method took 7.52 seconds for an imax of 10 millions.
在我的电脑上,这个老方法用了7.52秒的时间进行了1000万的imax放映。
The new method took 5.29 seconds for an imax of also 10 millions, which signifies a performance increase of over 20% (for 10 millions tries, obviously this percentage has a direct relationship to the number of tries)
这个新方法花费了5.29秒的时间,imax也有1000万,这意味着性能增长超过20%(1000万尝试,很明显这个百分比与尝试的数量有直接关系)
#5
0
this some kind of late but anyway and it is also somewhat tricky
这有点晚了,但不管怎样,这也有点棘手。
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'
review and tell me if this can work or initially you save all data in string and later split for array
回顾并告诉我这是否可行,或者最初你将所有数据保存在字符串中,然后将其拆分为数组。
#6
0
Not an answer Or Why 'tricky' is bad:
没有一个答案或者为什么“棘手”是不好的:
>> a = Array(1)
>> a = Split(Join(a, "||") & "||2", "||")
>> WScript.Echo a(0) + a(1)
>>
12