2010-10-19 20 views
5

Estoy trabajando en una función array_merge para ASP clásico. Lo que tengo parece estar funcionando, hasta que uno (o ambos) params estén vacíos o no arrays. Esto es lo que tengo hasta ahora:combinación de matriz en ASP clásico

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 

me sale el error:

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

Desde la línea de merged(right_size + i + 1) = right(i). ¿Alguna idea de dónde me estoy equivocando?

+0

¿Cuál fue la entrada cuando recibió ese error? –

+2

Simplemente lanzando esto: 'LEFT' y' RIGHT' son funciones en VBScript. ¿Es posible que el código en realidad esté barfing en 'right (i)'? Es posible que desee cambiar los nombres de los parámetros de la función. – LittleBobbyTables

Respuesta

5

LittleBobbyTables es correcto, debe cambiar los parámetros.

Creo que dependiendo de que introduce una verificación adicional para el objeto podría resolver su problema

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 

Algunos 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") 
+0

En lugar de iterar por la izquierda, ¿por qué no 'redim preserve left (total_size)' y luego le agregue los valores de la derecha? –

0

Sé que esta pregunta es un poco viejo, pero hay algo que necesita corregirlo, para que pueda obtener todos los valores de las dos matrices.

Necesita actualizar el contador dentro del segundo FOR, como lo hizo en el primero. De lo contrario, uno de los valores de la segunda matriz no se asignará.

Tome este código como un ejemplo:

''//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 
+0

Estoy confundido, ¿es esta una pregunta o una respuesta? – Lankymart

+0

¿No está claro que este es un reemplazo propuesto para el código del OP, que resuelve el problema? Declara una función llamada array_merge, y la respuesta dice "necesitas hacer tal y tal para lograr tal o cual cosa". No he revisado el código en detalle, pero no estoy seguro de por qué está marcado. Tal vez si reemplazamos "nosotros" con "usted" sería más claro que esta es una respuesta? – GreenAsJade

+0

@GreenAsJade Gracias por notar que esto es una respuesta y editarlo para que quede más claro. Perdón por el error al revisar publicaciones marcadas. –

0

mejora la eficiencia Pequeño a la respuesta de Paolo pesetas. No hay necesidad de iterar a través de arr1; solo "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