Consulting

Results 1 to 10 of 10

Thread: 2D arrays

  1. #1

    2D arrays

    In the sub called 'test' I have created a 2D array, I also have a utility method called arrayToString which simpley concats all elements of the array and returns it. Is it possible to obtain a sub 1D array from this 2D array and send it to arrayToString. Something like

    Debug.Print arrayToString(arr(2 to 1, 10 to 1))

     
    Public Sub test()
        Dim arr(1 To 10, 1 To 10) As Integer
        Dim i As Integer
        Dim j As Integer
        For i = LBound(arr) To UBound(arr)
            For j = LBound(arr) To UBound(arr)
                arr(i, j) = i * j
            Next j
        Next i
        
        Debug.Print arrayToString(arr(2 to 1, 10 to 1))
        
    End Sub
     
    Private Function arrayToString(ByRef arr() As Integer) As String
        Dim i As Integer
        Dim str As String
        
        str = "["
        For i = LBound(arr) To UBound(arr)
            str = str & arr(i) & ","
        Next i
        arrayToString = Left(str, Len(str) - 1) + "]"
    End Function

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Explain a bit more what byou want to do, and what you want to do with the array.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Ok to try and explaing a little differently, if I have a 2d array such as

    1,1,1,1
    2,2,2,2
    3,3,3,3
    4,4,4,4
    How can I get the 1D array '2,2,2,2'

    Thanks

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I thought that was what you would be wanting.

    Either

    ary1D = Application.Index(ary, 2, 0)

    or

    ary1D = Application.Transpose(Application.Index(ary, 0, 2))

    depending on whether you want rows or columns
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Great, that is what I am looking for.

    Ps. Is there a vba api anywhere?

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    API? What do youb mean? There is automation from other VBA hosting products.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    I am trying to use the suggested method Application.index(arr,i,j), however i keep getting the following error

    Type mismatch: array of user definer type expected

    What is goign wrong??

     
    Public Sub test()
        Dim arr() As Integer
        arr = create2DArray()
        Debug.Print IsArray(arr) ' Returns true
        Debug.Print IsArray(Application.Index(arr, 2, 0)) ' Returns true
        
        'Type mismatch: array of user definer type expected??
        Debug.Print (arrayToString(Application.Index(arr, 2, 0)))
        
    End Sub
     
     'This takes a string and returns a string reprenstation delimited by commas e.g [1,2,3]
    Private Function arrayToString(ByRef arr() As Integer) As String
        Dim i As Integer
        Dim str As String
        
        str = "["
        For i = LBound(arr) To UBound(arr)
            str = str & arr(i) & ","
        Next i
        arrayToString = Left(str, Len(str) - 1) + "]"
    End Function
     
    'creates and returns a 2d integer array
    Private Function create2DArray() As Integer()
        Dim arr(0 To 10, 0 To 10) As Integer
        Dim i As Integer
        Dim j As Integer
        For i = LBound(arr) To UBound(arr)
            For j = LBound(arr) To UBound(arr)
                arr(i, j) = i * j
            Next j
        Next i
        create2DArray = arr
    End Function

    PS an api is a big list of all pre defined classes and methods, check out the java one http://download.oracle.com/javase/1.4.2/docs/api/

    Thanks in adavnce

    Chris

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I know what an API is,l but I am asking what you mean by a VBA API. VBA is hosted, so is doesn't need an API, but maybe you mean tha Object Model?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    As to your problem

    [vba]


    Public Sub test()
    Dim arr() As Integer
    arr = create2DArray()
    Debug.Print IsArray(arr) ' Returns true
    Debug.Print IsArray(Application.Index(arr, 2, 0)) ' Returns true

    'Type mismatch: array of user definer type expected??
    Debug.Print (arrayToString(Application.Index(arr, 2, 0)))

    End Sub

    'This takes a string and returns a string reprenstation delimited by commas e.g [1,2,3]
    Private Function arrayToString(ByRef arr As Variant) As String
    Dim i As Integer
    Dim str As String

    str = "["
    For i = LBound(arr) To UBound(arr)
    str = str & arr(i) & ","
    Next i
    arrayToString = Left(str, Len(str) - 1) + "]"
    End Function

    'creates and returns a 2d integer array
    Private Function create2DArray() As Integer()
    Dim arr(0 To 10, 0 To 10) As Integer
    Dim i As Integer
    Dim j As Integer
    For i = LBound(arr) To UBound(arr)
    For j = LBound(arr) To UBound(arr)
    arr(i, j) = i * j
    Next j
    Next i
    create2DArray = arr
    End Function
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  10. #10
    Thanks for pointing out my mistake, I see you have changed integer to variant as Application.index method returns a variant array. This is why an API would be helpful as you would be able to see the return type of methods etc

    Now I want to use this to print out my 2D array, this work fine where i declare my array with even dimension but if I was to change my array declaration to the below it gives a runtime error??

    Dim arr(1 To 10, 1 To 5) As Integer



     
    Public Sub test()
        print2DArray create2DArray()
    End Sub
     
     'This takes a string and returns a string reprenstation delimited by commas e.g [1,2,3]
    Private Function arrayToString(ByRef arr() As Variant) As String
        Dim i As Integer
        Dim str As String
        str = "["
        For i = LBound(arr) To UBound(arr)
            str = str & Format(Val(arr(i)), "000") & ","
        Next i
        arrayToString = Left(str, Len(str) - 1) + "]"
    End Function
     
    'creates and returns a 2d integer array
    Private Function create2DArray() As Integer()
        Dim arr(1 To 10, 1 To 10) As Integer
        Dim i As Integer
        Dim j As Integer
        For i = LBound(arr) To UBound(arr)
            For j = LBound(arr) To UBound(arr)
                arr(i, j) = i * j
            Next j
        Next i
        create2DArray = arr
    End Function
    'This used the method arrayToString to print out 2D arrays
    Private Function print2DArray(ByRef arr() As Integer)
        Dim i As Integer
        Dim varr() As Variant
        For i = LBound(arr) To UBound(arr)
            varr = Application.Index(arr, i, 0)
            Debug.Print arrayToString(varr)
        Next i
    End Function
    Thanks again

    Chris

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •