Results 1 to 4 of 4

Thread: [RESOLVED] Copying from one array to another

  1. #1

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755

    Resolved [RESOLVED] Copying from one array to another

    I have an array that might look like this:
    1111111111
    1111111111
    1111111111
    1111111111
    1111111111

    And another that might look like this:
    22222
    22222
    22222

    What's the fastest way to copy the last array into the first, so it looks like this?
    1111111111
    1112222211
    1112222211
    1112222211
    1111111111



    Thanks!
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,176

    Re: Copying from one array to another

    Here's an example using the CopyMemory API which should improve performance,
    especially on larger arrays as it can copy entire rows at once.
    VB Code:
    1. Option Explicit
    2.  
    3. ' Win32 API Declaration
    4. Private Declare Sub CopyMemory Lib "kernel32" Alias _
    5.   "RtlMoveMemory" (Destination As Any, Source As Any, _
    6.   ByVal Length As Long)
    7.  
    8. Private Sub Command1_Click()
    9.   ' Create 2 2D Arrays
    10.   Dim bytArrayOne(9, 4) As Byte  ' 10x5
    11.   Dim bytArrayTwo(4, 2) As Byte  '  5x3
    12.  
    13.   ' Fill the arrays with a value;
    14.   ' 1's for the first, 2's for the second
    15.   Call Fill2DByteArray(bytArrayOne, 1)
    16.   Call Fill2DByteArray(bytArrayTwo, 2)
    17.  
    18.   ' Display the Arrays before the merge/copy
    19.   Debug.Print "Before:"
    20.   Call Display2DByteArray(bytArrayOne)
    21.   Call Display2DByteArray(bytArrayTwo)
    22.  
    23.   ' Merge the smaller array into the larger array,
    24.   ' offsetting it to be 4 columns in and 2 rows down
    25.   Call Merge2DByteArrays(bytArrayTwo, bytArrayOne, 3, 1)
    26.  
    27.   ' Display the larger array after the merge/copy
    28.   Debug.Print "After:"
    29.   Call Display2DByteArray(bytArrayOne)
    30. End Sub
    31.  
    32. Private Sub Merge2DByteArrays( _
    33.   ByRef bytSource() As Byte, ByRef bytDestination() As Byte, _
    34.   ByVal offsetX As Long, ByVal offsetY As Long)
    35.    
    36.   Dim lngRow As Long
    37.  
    38.   ' Copy the source array into the destination
    39.   ' array, one row at a time
    40.   For lngRow = LBound(bytSource, 2) To UBound(bytSource, 2)
    41.  
    42.     ' Copy the entire row of array entries into the
    43.     ' destination array, offsetting it by the specified coords
    44.     CopyMemory _
    45.       bytDestination(offsetX, offsetY), _
    46.       bytSource(LBound(bytSource, 1), lngRow), _
    47.       UBound(bytSource, 1) + (1 - LBound(bytSource, 1))
    48.    
    49.     ' Increment the row offset, ready for the next row
    50.     offsetY = offsetY + 1
    51.   Next
    52. End Sub
    53.  
    54. Private Sub Display2DByteArray(ByRef bytArray() As Byte)
    55.   Dim lngCol As Long
    56.   Dim lngRow As Long
    57.  
    58.   ' Display the 2D array in the Immediate window
    59.   For lngRow = LBound(bytArray, 2) To UBound(bytArray, 2)
    60.     For lngCol = LBound(bytArray, 1) To UBound(bytArray, 1)
    61.       Debug.Print bytArray(lngCol, lngRow);
    62.     Next
    63.     Debug.Print
    64.   Next
    65.   Debug.Print
    66. End Sub
    67.  
    68. Private Sub Fill2DByteArray(ByRef bytArray() As Byte, ByVal value As Byte)
    69.   Dim lngCol As Long
    70.   Dim lngRow As Long
    71.  
    72.   ' Populate the 2D array with the given value
    73.   For lngRow = LBound(bytArray, 2) To UBound(bytArray, 2)
    74.     For lngCol = LBound(bytArray, 1) To UBound(bytArray, 1)
    75.       bytArray(lngCol, lngRow) = value
    76.     Next
    77.   Next
    78. End Sub
    Regards,

    - Aaron.

  3. #3
    VB Guru ganeshmoorthy's Avatar
    Join Date
    Dec 2005
    Location
    Sharjah, United Arab Emirates
    Posts
    3,031

    Re: Copying from one array to another

    aaron : Nice coding and lots of new things i learnt from your code...
    If an answer to your question has been helpful, then please, Rate it!

    Have done Projects in Access and Member management systems using BioMetric devices, Smart cards and BarCodes.


  4. #4

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755

    Re: Copying from one array to another

    Thanks for that code!

    However I had to make the functions a bit more complex since the offset can be nagative sometimes in my program. And in that case the program should ignore any rows/colums that are out of bounds.

    This code was for a BitBlt-like function.
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

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