加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

arrays – ASP经典中的数组合并

发布时间:2020-12-16 07:02:51 所属栏目:asp.Net 来源:网络整理
导读:我正在为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)
我正在为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

我收到错误:

Error Type:
Microsoft VBScript runtime (0x800A01B6)
Object doesn't support this property or method: 'merged'
/_inc/nav/left-nav.inc,line 21

从合并的行(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

一些测试代码:

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")

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读