Search code examples
loopsvbscript

Adding 2 Arrays of dynamic length to 3rd Array


I have a problem with adding these 2 arrays together.

I have arr1 and arr2 this string elements.

    arr1(0) = "000"
    arr1(1) = "001"
    arr1(2) = "002"
    arr1(3) = "003"
    arr1(4) = "004"
    arr1(5) = "005"
    arr2(0) = "Closed"
    arr2(1) = "Open"

These arrays can be in dynamic size and values. (just added static values for demonstration)

I want to copy these items values to arr3. The output of arr3 should look like this:

arr3(0)  = "000 Closed"
arr3(1)  = "000 Open"
arr3(2)  = "001 Closed"
arr3(3)  = "001 Open"
arr3(4)  = "002 Closed"
arr3(6)  = "002 Open"
arr3(7)  = "003 Closed"
arr3(8)  = "003 Open"
arr3(9)  = "004 Closed"
arr3(10) = "004 Open"
arr3(11) = "005 Closed"
arr3(12) = "005 Open"

I tried this:

   Dim arr1(6)
   Dim arr2(2)
   Dim arr3(0)
   Dim i
   Dim y
   
   arr1(0) = "This000"
   arr1(1) = "This001"
   arr1(2) = "This002"
   arr1(3) = "This003"
   arr1(4) = "This004"
   arr1(5) = "This005"
   
    arr2(0) = "Closed"
    arr2(1) = "Open"
   
   redim  arr3 ((UBound(arr1)) * (Ubound(arr2)))
   
        Console.WriteLine (Ubound (arr3))
    
   for i =  Lbound(arr1) to Ubound(arr1)
                      
               for y = Lbound(arr2) to Ubound(arr2)
                arr3(i*y+y)       = (arr1(i) & arr2(y))
             next
  next

 for i =  Lbound(arr3) to Ubound(arr3)
    Console.WriteLine (arr3(i))
 next `

but I can't get it to work properly. Maybe you guys can help me? thanks

edit: I now got it kinda working. But i guess there is a more elegant solution to it than this:

Function GetParameters(ByRef arr1, ByRef arr2)
    Dim arr3()
    Dim i,j,k,l
    ReDim Preserve  arr3 ((UBound(arr1)+1) * (UBound(arr2)+1)-1)
    
    HMIRuntime.Trace (UBound (arr3)) &vbNewLine     
    
    
    l=0
    For  i=0 To UBound(arr3)
        If i > 0 Then
            i=i-1
        End If
        
        For j = 0 To UBound (arr2)
            
            For k = 0 To UBound(arr2)                  
                
                arr3(i) = arr1(l) & " " & arr2(k)                   
                
                        If i < UBound(arr3) Then
                            i=i+1
                        End If                 
                    Next
            If l < (UBound(arr1)) Then
                l=l+1               
            End If    
        Next
    Next
    GetParameters=arr3
End Function

edit2: Because resizing the array in the for loop got me into performance issues, I took a second attempt. Here is my final solution (in case somebody else is interested):

Dim arr1()
Dim arr2()
Dim arr3()
Dim i,j,k,l,m,n
Dim maxArr1
Dim maxArr2
maxArr1 = 5 'change for dynamic Arr
maxArr2 = 10

j=0

for l = 0 to maxArr1 'TestArray1
  ReDim Preserve arr1(l)
    arr1(l) = Cstr(l)  
 
Next


   for m = 0 to maxArr2  'TestArray2
    ReDim Preserve arr2(m)
    arr2(m) ="Closed_"& Cstr(m) 
  
  Next

   ReDim Preserve arr3( (((Ubound(arr1)+1) * (Ubound(arr2)+1) )-1) ) 'New size for array 3

'Copy arr1 and arr2 in arr3 (according to pattern)
for i=0 to UBound(arr1)   
    for k=0 to Ubound(arr2)   
      arr3(j)=arr1(i)& " - "&arr2(k)
        j=j+1
     Next
Next

For n = 0 To UBound(arr3)
  WScript.Echo "arr3("&n&") = "&arr3(n)
Next

Solution

  • Here's a quick attempt based on your code that uses Preserve to maintain the array and build elements dynamically.

    Dim arr1(6)
    Dim arr2(2)
    Dim arr3()
    Dim i
    Dim y
    
    arr1(0) = "000"
    arr1(1) = "001"
    arr1(2) = "002"
    arr1(3) = "003"
    arr1(4) = "004"
    arr1(5) = "005"
    
    arr2(0) = "Closed"
    arr2(1) = "Open"
    
    Dim item, x
    For i = 0 To UBound(arr1) - 1
      item = arr1(i)
      For y = 0 To UBound(arr2) - 1
        x = i + y + 1
        ReDim Preserve arr3 (i + x)
        arr3(i + x) = item & " " & arr2(y)
      Next
    Next
    
    For i = 0 To UBound(arr3)
      WScript.Echo arr3(i)
    Next
    

    Output:

    000 Closed
    000 Open
    001 Closed
    001 Open
    002 Closed
    002 Open
    003 Closed
    003 Open
    004 Closed
    004 Open
    005 Closed
    005 Open