繁体   English   中英

ASP经典中的数组合并

[英]Array merge in ASP classic

我正在研究 ASP 经典的 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) 关于我哪里出错的任何智慧?

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

Paolo 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

我知道这个问题有点老了,但是您需要解决一些问题,以便您可以从两个数组中获取所有值。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM