从字典中检索数组似乎会导致深层复制
Retrieving array from dictionary appears to result in a deep copy
以下代码将数组添加到字典中。数组的第一个元素被赋予默认值。然后,在字典中查找数组并将新值分配给第一个元素。然而,再次在字典中查找数组时,它仍然是默认值。
我已经通过将修改后的数组分配回字典来解决这个问题,但我担心的是查找和重写将导致数组的全部内容被复制两次。
是否可以使用对数组的引用?
<% Option Explicit
Function createArray
Dim newArray(10)
newArray(0) = "Hello"
createArray = newArray
End Function
Dim myDictionary, myArray
Set myDictionary = CreateObject("Scripting.Dictionary")
myDictionary.Add "MyItem", createArray
myArray = myDictionary("MyItem")
Response.Write myArray(0) 'Hello
myArray(0) = "World!"
Response.Write myArray(0) 'World!
myArray = myDictionary("MyItem")
Response.Write myArray(0) 'Hello
%>
在 VBScript(经典 ASP)中,数组不是对象,因此您也不要使用 Set 运算符来复制数组。
按值复制数组(即深层复制)是合乎逻辑的(以 VBScript 的方式)。
作为快速参考,我只能在 JScript 和 VBScript 数组之间找到 this comparison。引用:
A VBScript array is:
multi-dimensional
indexed by integer tuples
dense
not an object
而 JScript 数组是:
one dimensional.
associative; JScript arrays are indexed by strings. Numeric indices are actually converted to strings internally.
sparse: arr1 = 123; arr[1000000] = 456; gives you a two-member array, not a million-member array.
an object with properties and methods.
以下代码将数组添加到字典中。数组的第一个元素被赋予默认值。然后,在字典中查找数组并将新值分配给第一个元素。然而,再次在字典中查找数组时,它仍然是默认值。
我已经通过将修改后的数组分配回字典来解决这个问题,但我担心的是查找和重写将导致数组的全部内容被复制两次。
是否可以使用对数组的引用?
<% Option Explicit
Function createArray
Dim newArray(10)
newArray(0) = "Hello"
createArray = newArray
End Function
Dim myDictionary, myArray
Set myDictionary = CreateObject("Scripting.Dictionary")
myDictionary.Add "MyItem", createArray
myArray = myDictionary("MyItem")
Response.Write myArray(0) 'Hello
myArray(0) = "World!"
Response.Write myArray(0) 'World!
myArray = myDictionary("MyItem")
Response.Write myArray(0) 'Hello
%>
在 VBScript(经典 ASP)中,数组不是对象,因此您也不要使用 Set 运算符来复制数组。
按值复制数组(即深层复制)是合乎逻辑的(以 VBScript 的方式)。
作为快速参考,我只能在 JScript 和 VBScript 数组之间找到 this comparison。引用:
A VBScript array is:
multi-dimensional
indexed by integer tuples
dense
not an object而 JScript 数组是:
one dimensional.
associative; JScript arrays are indexed by strings. Numeric indices are actually converted to strings internally.
sparse: arr1 = 123; arr[1000000] = 456; gives you a two-member array, not a million-member array.
an object with properties and methods.