Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array merge in ASP classic

I'm working on an array_merge function for ASP classic. What I have seems to be working, until one (or both) params are empty or not arrays. Here's what I have so far:

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

I get the error:

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

From the line merged(right_size + i + 1) = right(i). Any wisdom on where I'm going wrong?

like image 290
Chris Tonkinson Avatar asked Oct 19 '10 16:10

Chris Tonkinson


2 Answers

LittleBobbyTables is right, you should change the parameters.

I think depending on you inputs an extra check for object could solve your issue

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

Some 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")
like image 51
Yots Avatar answered Sep 28 '22 03:09

Yots


Small efficiency improvement to Paolo Pta's answer. There's no need to iterate through arr1; just "redim preserve" it.

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
like image 30
Stephen R Avatar answered Sep 28 '22 02:09

Stephen R