Results 1 to 6 of 6

Thread: Add new element and rearrange an array

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    15

    Resolved Add new element and rearrange an array

    Hi alls,
    I have an array of real number like as:
    1.1 ; - 2.1 ; 3.1 ; - 1.0 ; 3.0 ; 5.1 ; 0.2 ; 0.5

    Now, I want to insert two elements (1.1 ; 0.1) into the array right after third element. the elements before third element will insert the end of this array. The final array will be as:
    1.1 ; 0.1 ; - 1.0 ; 3.0 ; 5.1 ; 0.2 ; 0.5 ; 1.1 ; - 2.1 ; 3.1

    Could you please tell me how i can do this in VB6. Thank you very much.
    Last edited by anhdung; Feb 13th, 2009 at 09:07 AM.

  2. #2
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: Add new element and rearrange an array

    I have a similar procedure i use, you can probably modify it to fit your needs:
    Code:
    Option Explicit
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    
    Private Sub Command1_Click()
    Dim x() As Integer, y() As Integer
        Dim i As Integer, j As Integer
       
        ReDim x(1 To 3, 1 To 10)
       
            Debug.Print "Old array"
            For i = 1 To 3
                For j = 1 To 10
                    x(i, j) = i * j
                    Debug.Print i & " " & j & " " & x(i, j),
                Next
                Debug.Print
            Next
           
            Debug.Print "New array"
           
            ReDim y(1 To 4, 1 To 10)
           
           
            For i = 1 To 10
                CopyMemory y(1, i), x(1, i), 3 * 2
            Next
           
            For i = 1 To 4
                 For j = 1 To 10
                    Debug.Print i & " " & j & " " & y(i, j),
                Next
                Debug.Print
            Next
    
    
    End Sub
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    15

    Re: Add new element and rearrange an array

    Thank you very much.

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Add new element and rearrange an array

    here is another method
    vb Code:
    1. Sub addto(ByRef sarr() As String, narr() As String, pos As Integer)
    2. tmp = sarr
    3. j = UBound(narr)
    4. ReDim sarr(UBound(sarr) + j + 1)
    5. For i = 0 To UBound(narr)
    6.     sarr(i) = narr(i)
    7. Next
    8. For i = pos To UBound(tmp)
    9.     j = j + 1
    10.     sarr(j) = tmp(i)
    11. Next
    12. For i = 0 To pos - 1
    13.     j = j + 1
    14.     sarr(j) = tmp(i)
    15. Next
    16. End Sub
    call like
    vb Code:
    1. Dim strarr() As String, addarr() As String
    2. mstr = "1.1 ; - 2.1 ; 3.1 ; - 1.0 ; 3.0 ; 5.1 ; 0.2 ; 0.5"
    3. strarr = Split(mstr, ";")
    4. addarr = Split("1.1; 0.1", ";")
    5. addto strarr, addarr, 3
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    15

    Re: Add new element and rearrange an array

    Thank westconn1 very much. I modified your code for add new element in array of real number. It worked well.

    I have a question about CopyMemory function, the first time I use this method.

    Code:
    Option Explicit
    
    ' Declare the memory copying funciton.
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    
    Private Sub Command1_Click()
    
    Dim array1() As Single
    ReDim array1(8) As Single
    
    Dim array2() As Single
    ReDim array2(10) As Single
    Dim i As Long, j As Long, l As Long
    Dim k As Long
    
    For i = 1 To 8
          ReDim Preserve array1(i) As Single
          array1(i) = i
          List1.AddItem array1(i)
    Next i
    
    array2(1) = 0.1
    array2(2) = 0.2
    
    For j = 3 To 7
          CopyMemory array2(j), array1(j + 1), 4
    Next j
    
    For l = 8 To 10
         CopyMemory array2(l), array1(l - 7), 4
    Next l
    
    
    For k = 1 To UBound(array2) 
          List2.AddItem array2(k)
    Next k
    
    End Sub
    I don't understand the use of ByVal Length As Long in CopyMemory. Could you please explain to me why you used 3 * 2 for your code.

    In my code If i use ByVal Length As Long less than 4, new array will return an array like as 0.1 0.2 0 0 0 0 0 0 0 0

    But I change ByVal Length As Long larger than 4, new array will return an array I want
    0.1 0.2 4 5 6 7 8 1 2 3
    Last edited by anhdung; Feb 13th, 2009 at 08:17 AM.

  6. #6
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Add new element and rearrange an array

    If I may speak on behalf of isnoend07, the 3 is because there are 3 elements and the 2 is because each Integer element is 2 bytes long.

    Be very careful with RtlMoveMemory (aka CopyMemory), as powerful as it is, it is not at all forgiving. One mistake will crash VB. If you do use it make sure you understand how VB holds it's variables in memory, and SAVE YOUR WORK before running the code.

    There is also a small overhead associated with calling API making CopyMemory not so efficient for copying small amounts of Data.

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