ASP经典中的数组合并
我正在处理ASP经典的array_merge函数。我似乎正在工作,直到一个(或两个)参数为空或不是数组。这是我到目前为止有:ASP经典中的数组合并
function array_merge(left, right)
dim total_size
dim i
dim merged
' Convert "left" to an array
if not isArray(left) then
left = Array(left)
end if
' Convert "right" to an array
if not isArray(right) then
right = Array(right)
end if
' Start with "left" and add the elements of "right"
right_size = ubound(right)
total_size = ubound(left) + right_size + 1
merged = left
redim preserve merged(total_size)
for i = 0 to ubound(right)
merged(right_size + i + 1) = right(i)
next
' Return value
array_merge = merged
end function
我得到的错误:
Error Type: Microsoft VBScript runtime (0x800A01B6) Object doesn't support this property or method: 'merged' /_inc/nav/left-nav.inc, line 21
从线merged(right_size + i + 1) = right(i)
。任何智慧我要去哪里错了?
LittleBobbyTables是正确的,你应该改变参数。
我觉得这取决于你输入的对象额外的检查可以解决您的问题
function array_merge(left, right)
dim right_size
dim total_size
dim i
dim merged
''// Convert "left" to an array
if not isArray(left) then
left = Array(left)
end if
''// Convert "right" to an array
if not isArray(right) then
right = Array(right)
end if
''// Start with "left" and add the elements of "right"
right_size = ubound(right)
total_size = ubound(left) + right_size + 1
merged = array()
redim merged(total_size)
dim counter : counter = 0
for i = lbound(left) to ubound(left)
if isobject(left(i))then
set merged(counter) = left(i)
else
merged(counter) = left(i)
end if
counter=counter+1
next
for i = lbound(right) to ubound(right)
if isobject(right(i))then
set merged(counter) = right(i)
else
merged(counter) = right(i)
end if
next
''// Return value
array_merge = merged
end function
一些Testcode:
dim a: a=100
dim b: b=200
dim c: set c=nothing
dim d: set d=nothing
dim e: set e=server.createobject("scripting.filesystemobject")
dim f: set f=server.createobject("scripting.filesystemobject")
dim x,y,z,zz
x = array_merge(a,b)
y = array_merge(c,d)
z = array_merge(e,f)
zz = array_merge(a,e)
response.write x(0)
response.write x(1)
''// Accessing Nothing Values throw Error
''//response.write y(0)
''//response.write y(1)
response.write z(0).GetExtensionName("test.doc")
response.write z(1).GetExtensionName("test.doc")
response.write zz(0)
response.write zz(1).GetExtensionName("test.doc")
而不是通过左侧迭代,为什么不'redim保留左(total_size)',然后将正确的值添加到它? – 2017-08-17 14:19:38
我知道这个问题是有点老了,但有的东西您需要修复,以便您可以从两个数组中获取所有值。
您需要升级第二个FOR内部的计数器,就像您在第一个FOR中那样。 否则将不会分配第二个数组中的一个值。
拿这个代码为例:
''//Build the Arrays
Dim a,b,c
a=array("a1","a2") : b=array("b1","b2") : c=array_merge(a,b)
''//Run the code
For Each i In c
Response.Write i &"<br />"
Next
''//The main function
Function array_merge(arr1, arr2)
''//Declare all function variables
dim arr1_size,arr2_size,total_size,i,merged,counter
''//Fix empty or none arrays
if not isArray(arr1) then arr1 = Array(arr1) end if
if not isArray(arr2) then arr2 = Array(arr2) end if
''// Get and set the Arrays Size
arr1_size = ubound(arr1) : arr2_size = ubound(arr2)
total_size = arr1_size + arr2_size + 1
''//Create a temporary array and assign it a size
merged = array()
redim merged(total_size)
counter = 0
''//Create one single Array with the two others by looping them
For i = lbound(arr1) to ubound(arr1)
IF isobject(arr1(i)) then
set merged(counter) = arr1(i)
Else
merged(counter) = arr1(i)
End if
counter=counter+1
Next
For i = lbound(arr2) to ubound(arr2)
If isobject(arr2(i))then
set merged(counter) = arr2(i)
Else
merged(counter) = arr2(i)
End if
counter=counter+1
Next
''// Return the value
array_merge = merged
End Function
我很困惑这是一个问题还是答案? – Lankymart 2014-12-24 20:46:42
是不是很清楚这是OP代码的一个建议替代品,它解决了这个问题?它声明了一个名为array_merge的函数,答案是“你需要做某些事情来实现这样的目标”。我没有详细审查代码,但我不清楚它为什么甚至被标记?也许如果我们用“你”代替“我们”,这将是更清楚的,这是一个答案? – GreenAsJade 2014-12-25 01:33:46
@GreenAsJade感谢您注意到这是一个答案,并对其进行编辑,使其更清晰。对于查看标记的帖子时出现的错误感到抱歉。 – 2014-12-25 03:11:09
小效率提高到保罗PTA的答案。没有必要遍历arr1;只是“redim保存”它。
Function array_merge(arr1, arr2)
dim arr1_size, arr2_size, total_size, i, counter
if not isArray(arr1) then arr1 = Array(arr1)
if not isArray(arr2) then arr2 = Array(arr2)
arr1_size = ubound(arr1) : arr2_size = ubound(arr2)
total_size = arr1_size + arr2_size + 1
counter = arr1_size + 1
Redim Preserve arr1(total_size)
For i = lbound(arr2) to arr2_size
If isobject(arr2(i))then
set arr1(counter) = arr2(i)
Else
arr1(counter) = arr2(i)
End if
counter = counter + 1
Next
array_merge = arr1
End Function
当你发现错误时,输入是什么? – 2010-10-19 16:28:34
只是抛出了这个 - 'LEFT'和'RIGHT'是VBScript中的函数。是否有可能代码实际上是在“正确的(i)”上?您可能想要更改函数的参数名称。 – LittleBobbyTables 2010-10-19 16:31:07