Results 1 to 6 of 6

Thread: to add rows in array two dimensions

  1. #1

    Thread Starter
    Fanatic Member mutley's Avatar
    Join Date
    Apr 2000
    Location
    Sao Paulo - Brazil
    Posts
    709

    Question to add rows in array two dimensions

    Hi

    I would like to create a array two dimension like below


    +-----------+----------+----------+
    | Titulo | Valor1 | Valor2 |
    +-----------|----------|----------|
    | Titulo1 | 10 | 5 |
    | Titulo2 | 20 | 10 |
    +-----------+----------+----------+

    I tried to use code below, but return conversion type, somebody have some other idea How can I do It


    Code:
         Private Sub Form_Load()
             Dim myArray() As String
             ReDim myArray(2, 0)
             Add myArray, "Titulo01", "10", "5"
             Add myArray, "Titulo02", "20", "10"
             myArray = TransposeArray(myArray)
         
         
         End Sub
         Private Sub Add(ByRef myArray() As String, PTitulo As String, valor1 As String, valor2 As String)
             If Not (myArray(0, 0) = vbNullString And myArray(1, 0) = vbNullString) Then
                 ReDim Preserve myArray(2, UBound(myArray, 2) + 1)
             End If
             myArray(0, UBound(myArray, 2)) = PTitulo
             myArray(1, UBound(myArray, 2)) = valor1
             myArray(2, UBound(myArray, 2)) = valor2
         
         
         
         
         End Sub
         Public Function TransposeArray(myArray As Variant) As Variant
         
             Dim X As Long
             Dim Y As Long
             Dim Xupper As Long
             Dim Yupper As Long
             Dim tempArray As Variant
             Xupper = UBound(myArray, 2)
             Yupper = UBound(myArray, 1)
             ReDim tempArray(Xupper, Yupper)
             For X = 0 To Xupper
                 For Y = 0 To Yupper
                     tempArray(X, Y) = myArray(Y, X)
                 Next Y
             Next X
             TransposeArray = tempArray
         End Function

  2. #2
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,041

    Re: to add rows in array two dimensions

    not sure if this is what you want

    this will take a Textfile and Transpose
    Code:
    Private Sub Command1_Click()
    Const OutputCSV = "E:\outtrans.txt"
    Dim outStream
    Dim row As Variant
    Dim ent As Variant
    Dim dt_start, WriteOutput: dt_start = Now
    Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
    Dim file: Set file = fso.OpenTextFile("E:\RotationTest2.txt", 1, True)
    Set WriteOutput = fso.OpenTextFile(OutputCSV, 8, True)
    Dim fc: fc = file.ReadAll: file.Close: Dim fcArray: fcArray = Split(fc, vbCrLf)
    
    Debug.Print "Before Transpose"
    Debug.Print "----------------"
    Debug.Print fc
    Debug.Print "----------------"
    
    Dim opArray(): ReDim opArray(0)
    For Each row In fcArray
        Dim tmp: tmp = Split(row, ",")
        For ent = 0 To UBound(tmp)
            If ent > UBound(opArray) Then
                ReDim Preserve opArray(UBound(opArray) + 1)
                opArray(ent) = Trim(tmp(ent))
            Else
                If Len(opArray(ent)) > 0 Then
                    opArray(ent) = opArray(ent) & "," & Trim(tmp(ent))
                Else
                    opArray(ent) = Trim(tmp(ent))
                End If
            End If
        Next
    Next
    Dim dt_end: dt_end = Now
    
    Debug.Print "After Transpose"
    Debug.Print "----------------"
    Debug.Print Join(opArray, vbCrLf)
    Debug.Print "----------------"
    Debug.Print "Script Execution Time (sec): " & DateDiff("s", dt_start, dt_end)
    
    WriteOutput.Write Join(opArray, vbCrLf): WriteOutput.Close
    Set outStream = fso.OpenTextFile(OutputCSV, 2, True) ' 2 = ForWriting
    'write Header
    outStream.writeLine Join(opArray, vbCrLf)
    'write new file
    'outStream.writeLine rst.GetString(2, , ",", vbCrLf, "")
    outStream.Close
    
    
    End Sub
    the output before and after
    Code:
    Before Transpose
    ----------------
    Rotation0,Rotation1,Rotation2,Rotation3,Rotation4,Rotation6,Rotation7,Rotation8,Rotation9,Rotation0,Rotation 11,Rotation12
    50,10,10,30,40,50,10,10,30,10,10,30
    40,50,10,10,70,60,80,10,10,80,10,10
    30,40,50,10,10,30,40,50,10,40,50,10
    10,30,40,50,10,10,30,40,50,30,40,50
    10,10,30,40,50,10,10,30,40,10,30,40
    
    ----------------
    After Transpose
    ----------------
    Rotation0,50,40,30,10,10
    Rotation1,10,50,40,30,10
    Rotation2,10,10,50,40,30
    Rotation3,30,10,10,50,40
    Rotation4,40,70,10,10,50
    Rotation6,50,60,30,10,10
    Rotation7,10,80,40,30,10
    Rotation8,10,10,50,40,30
    Rotation9,30,10,10,50,40
    Rotation0,10,80,40,30,10
    Rotation 11,10,10,50,40,30
    Rotation12,30,10,10,50,40
    ----------------
    Script Execution Time (sec): 0
    HTH
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  3. #3
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,417

    Re: to add rows in array two dimensions

    myArray = TransposeArray(myArray)
    Huh?
    Since when can you use a dimensioned Array to receive a result from a function?
    And your Type Mismatch is due to the "Variants"

    this one works
    Code:
    Private Sub Form_Load()
        Dim myArray() As String
        Dim myArray2() As String
        ReDim myArray(2, 0)
             Add myArray, "Titulo01", "10", "5"
             Add myArray, "Titulo02", "20", "10"
             myArray2 = TransposeArray(myArray)
         End Sub
         Private Sub Add(ByRef myArray() As String, PTitulo As String, valor1 As String, valor2 As String)
             If Not (myArray(0, 0) = vbNullString And myArray(1, 0) = vbNullString) Then
                 ReDim Preserve myArray(2, UBound(myArray, 2) + 1)
             End If
             myArray(0, UBound(myArray, 2)) = PTitulo
             myArray(1, UBound(myArray, 2)) = valor1
             myArray(2, UBound(myArray, 2)) = valor2
         
         
         
         
         End Sub
         Public Function TransposeArray(myArray() As String) As String()
         
             Dim X As Long
             Dim Y As Long
             Dim Xupper As Long
             Dim Yupper As Long
             Dim tempArray() As String
             Xupper = UBound(myArray, 2)
             Yupper = UBound(myArray, 1)
             ReDim tempArray(Xupper, Yupper)
             For X = 0 To Xupper
                 For Y = 0 To Yupper
                     tempArray(X, Y) = myArray(Y, X)
                 Next Y
             Next X
             TransposeArray = tempArray
         End Function
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  4. #4
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,224

    Re: to add rows in array two dimensions

    Quote Originally Posted by Zvoni View Post
    Huh?
    Since when can you use a dimensioned Array to receive a result from a function?
    And your Type Mismatch is due to the "Variants"
    Returning Arrays from functions was new to VB6. Beware though - MS kind of hacked it in, and wasn't fully tested before it made it to release - so it can be quite buggy in edge cases. Of course MS never fixed them before abandoning VB6 altogether. You probably don't want to use them on any public COM interfaces. I don't think that was part of the COM spec anyway.

    Here's what I recommend:
    Code:
    Public Function TransposeArray(ByVal MyArray As Variant) As Variant
    
        Dim X As Long
        Dim Y As Long
        Dim Xupper As Long
        Dim Yupper As Long
        'Dim tempArray As Variant
        Dim tempArray() As String
        
        Xupper = UBound(MyArray, 2)
        Yupper = UBound(MyArray, 1)
        ReDim tempArray(Xupper, Yupper)
        For X = 0 To Xupper
            For Y = 0 To Yupper
                tempArray(X, Y) = MyArray(Y, X)
            Next Y
        Next X
        TransposeArray = tempArray
    End Function

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: to add rows in array two dimensions

    Quote Originally Posted by DEXWERX View Post
    Beware though - MS kind of hacked it in, and wasn't fully tested before it made it to release - so it can be quite buggy in edge cases. Of course MS never fixed them before abandoning VB6 altogether. You probably don't want to use them on any public COM interfaces. I don't think that was part of the COM spec anyway.
    I'm not sure I buy any of this.

    It feels like one of those "It doesn't do what I want, therefore it is broken" opinions.

    Marshaling Details calls out arrays specifically:

    Different parameter types are marshaled in different ways. For example, marshaling an integer parameter involves simply copying the value into the message buffer. (Although even in this simple case, there are issues such as byte ordering to deal with in cross-computer calls.) Marshaling an array, however, is a more complex process. Array members are copied in a specific order so that the other side can reconstruct the array exactly. When a pointer is marshaled, the data that the pointer is pointing to is copied following rules and conventions for dealing with nested pointers in structures. Unique functions exist to handle the marshaling of each parameter type.
    I see no implication that arrays are not addressed in COM.

    It may be useful to enumerate specific things that don't seem to work as expected, but scaring people away from something so useful is silly. It reminds me of those Rulz Fer Noobz like "Always Set each object = Nothing." We can do better than that.

  6. #6
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,224

    Re: to add rows in array two dimensions

    Quote Originally Posted by dilettante View Post
    I'm not sure I buy any of this.
    It feels like one of those "It doesn't do what I want, therefore it is broken" opinions.
    Triggering an access violation with VB6's array syntax, is the definition of broken.
    Your "feels" however are still welcome.

    Quote Originally Posted by dilettante View Post
    Marshaling Details calls out arrays specifically:

    I see no implication that arrays are not addressed in COM.
    Yup - SAFEARRAYs have always marshalled correctly when packed in a VARIANT. Maybe someone other than me who's actually used DCOM can chime in on that. Here's a post about workarounds to passing SAFEARRAYs to COM event handlers. https://limbioliong.wordpress.com/20...andler-part-3/ you'll notice one of the workarounds mentioned, is to use a VARIANT.

    Quote Originally Posted by dilettante View Post
    It may be useful to enumerate specific things that don't seem to work as expected, but scaring people away from something so useful is silly. It reminds me of those Rulz Fer Noobz like "Always Set each object = Nothing." We can do better than that.
    That particular "Rulz Fer Noobz" was born out of an actual broken ADO / runtime issue. Granted it has been fixed for almost as long as VB6 has been around - It helps to know your history.

    It helps to make informed decisions on when it's appropriate to use a VARIANT vs a SAFEARRAY and when to explicitly Set Object = Nothing. It also helps to know the history when odd bugs creep in, and everything typical has been exhausted - you know where to look. I do appreciate dilettantes effort to elevate the discussion. Sometimes "get off my lawn" rubs people the wrong way, or worse falls on deaf ears - but I hear you.
    Last edited by DEXWERX; Jan 29th, 2019 at 01:12 PM. Reason: added link

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width