Chr*_*son 5 arrays merge function asp-classic
我正在为ASP classic工作一个array_merge函数.我有什么似乎工作,直到一个(或两个)参数是空的或不是数组.这是我到目前为止所拥有的:
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
Run Code Online (Sandbox Code Playgroud)
我收到错误:
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
Run Code Online (Sandbox Code Playgroud)
一些测试代码:
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")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3411 次 |
| 最近记录: |