gpt4 book ai didi

vbscript - 从字典中检索数组似乎会产生深层复制

转载 作者:行者123 更新时间:2023-12-02 19:32:42 24 4
gpt4 key购买 nike

以下代码将一个数组添加到字典中。数组的第一个元素被赋予默认值。然后,在字典中查找该数组,并将新值分配给第一个元素。然而,当再次在字典中查找该数组时,它仍然具有默认值。

我通过将修改后的数组分配回字典来解决这个问题,但我担心查找和重写将导致数组的整个内容被复制两次。

是否可以使用对数组的引用?

<% 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.

关于vbscript - 从字典中检索数组似乎会产生深层复制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34709614/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com