ASP经典中的数组合并

时间:2010-10-19 16:13:37

标签: arrays function asp-classic merge

我正在为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

merged(right_size + i + 1) = right(i)行开始。关于我哪里出错的任何智慧?

4 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:1)

Paolo Pta的答案提高了效率。没有必要遍历arr1;只是" redim preserve"它

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

答案 2 :(得分:0)

我知道这个问题有点陈旧,但您需要修复一些问题,以便您可以从两个数组中获取所有值。

您需要在第二个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

答案 3 :(得分:0)

几年后,但也许有人可以使用它。

function array_merge(arr, arr2)
    for each elm in arr2
        redim preserve arr(UBound(arr) + 1)
        arr(UBound(arr)) = elm
    next

    array_merge = arr
end function