|
-
Mar 16th, 2015, 01:10 PM
#1
Thread Starter
Fanatic Member
Duplicate array using copymemory
Hi
Is there some way to duplicate informations of the array , I tried to use a example found using copy memory, but in example only copy data array to other. I would like some as:
Myarray (0) = "A00"
Myarray (1) = "A00"
Myarray (2) = "B00"
Myarray (4) = "AB0"
When I call the function to duplicate array , I would like return me a array as:
Myarray (0) = "A00"
Myarray (1) = "A00"
Myarray (2) = "B00"
Myarray (4) = "AB0"
Myarray (5) = "A00"
Myarray (6) = "A00"
Myarray (7) = "B00"
Myarray (8) = "AB0"
See the code below , I tried, but not found a way
Code:
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" ( _
dest As Any _
, source As Any _
, ByVal bytes As Long)
Dim MyArr() As Long
Private Sub redimFast( _
ByRef theArray() As Long _
, ByVal LowBound As Long _
, ByVal UpperBound As Long)
' copy A() into B()
Dim tmpArr() As Long
ReDim tmpArr(UpperBound - LowBound)
CopyMemory tmpArr(0), theArray(LowBound), ((UpperBound - LowBound + 1) * Len(theArray(0)))
Erase theArray
theArray = tmpArr
End Sub
How to do it when my type as string
-
Mar 16th, 2015, 01:52 PM
#2
Re: Duplicate array using copymemory
Well, you have to change the sub header portion ByRef theArray() As Long to String instead of Long
same with tmparray you can't put strings into an array of longs
-
Mar 16th, 2015, 02:01 PM
#3
Thread Starter
Fanatic Member
Re: Duplicate array using copymemory
 Originally Posted by DataMiser
Well, you have to change the sub header portion ByRef theArray() As Long to String instead of Long
same with tmparray you can't put strings into an array of longs
Thank
I tried as below, but I do know How can duplicate
Code:
Private Type mytype
f01 As String * 3
s01 As String * 1
f02 As String * 3
s02 As String * 1
f03 As String * 3
s03 As String * 1
f04 As String * 3
s04 As String * 1
f05 As String * 3
s05 As String * 1
f06 As String * 3
s06 As String * 1
f07 As String * 3
s07 As String * 1
f08 As String * 3
s08 As String * 1
f09 As String * 3
s09 As String * 1
f10 As String * 3
s10 As String * 1
f11 As String * 3
s11 As String * 1
f12 As String * 3
s12 As String * 1
f13 As String * 3
s13 As String * 1
f14 As String * 3
End Type
Type mytype_full
t_reg As String * 55
End Type
Dim MyArr() As Long
Dim Arr1(0 To 1000) As mytype_full
Dim Arr2(0 To 1000) As mytype
ARR2(0)="AAB AAB AAC AAD AAB AAC CCC AAB AAB AAC AAD AAB AAC CCC"
How can I to duplicate ?
-
Mar 16th, 2015, 02:08 PM
#4
Re: Duplicate array using copymemory
Problem you must understand with strings is that you are not just copying byte data, VB string arrays are arrays of BSTRs. Each string must be allocated. But all is naught since you appear to not be copying consecutive ranges of array items, but skipping some, i.e., (0),(1),(2),(4)
If you are copying consecutive array elements, then this can be done, but honestly shouldn't be too much faster than (if at all, depending on size of array) than the following without using copymemory:
Code:
Dim lCount As Long, UB as Long, LB as Long
LB = LBound(StringArray)
UB = UBound(StringArray)
lCount = Abs(UB - LB) + 1
ReDim Preserve StringArray(LB To LB + lCount * 2 - 1)
For UB = UB + 1 To UB + lCount
StringArray(UB) = StringArray(LB)
LB = LB + 1
Next
And with CopyMemory...
Code:
Private Declare Sub FillMemory Lib "kernel32" _
Alias "RtlFillMemory" ( _
dest As Any _
, ByVal Length As Long _
, ByVal Fill As Byte)
Dim lCount As Long, UB as Long, LB as Long
Dim tmpS() As String
LB = LBound(StringArray)
UB = UBound(StringArray)
lCount = Abs(UB - LB) + 1
tmpS = StringArray
ReDim Preserve StringArray(LB To LB + lCount * 2 - 1)
CopyMemory ByVal VarPtr(StringArray(LB + lCount)), ByVal VarPtr(tmpS(LB)), lCount * 4
FillMemory ByVal VarPtr(tmpS(LB)), lCount * 2, 0
Note: The LB & UB stuff in sample code is simply to handle any LBound vs. hardcoding for zero
The above is air-code, so apologize in advance if any typos
Edited: You added another post, different data. The above is provided for your original post #1.
Last edited by LaVolpe; Mar 16th, 2015 at 02:13 PM.
-
Mar 18th, 2015, 02:11 PM
#5
Thread Starter
Fanatic Member
Re: Duplicate array using copymemory
 Originally Posted by LaVolpe
Problem you must understand with strings is that you are not just copying byte data, VB string arrays are arrays of BSTRs. Each string must be allocated. But all is naught since you appear to not be copying consecutive ranges of array items, but skipping some, i.e., (0),(1),(2),(4)
If you are copying consecutive array elements, then this can be done, but honestly shouldn't be too much faster than (if at all, depending on size of array) than the following without using copymemory:
Code:
Dim lCount As Long, UB as Long, LB as Long
LB = LBound(StringArray)
UB = UBound(StringArray)
lCount = Abs(UB - LB) + 1
ReDim Preserve StringArray(LB To LB + lCount * 2 - 1)
For UB = UB + 1 To UB + lCount
StringArray(UB) = StringArray(LB)
LB = LB + 1
Next
And with CopyMemory...
Code:
Private Declare Sub FillMemory Lib "kernel32" _
Alias "RtlFillMemory" ( _
dest As Any _
, ByVal Length As Long _
, ByVal Fill As Byte)
Dim lCount As Long, UB as Long, LB as Long
Dim tmpS() As String
LB = LBound(StringArray)
UB = UBound(StringArray)
lCount = Abs(UB - LB) + 1
tmpS = StringArray
ReDim Preserve StringArray(LB To LB + lCount * 2 - 1)
CopyMemory ByVal VarPtr(StringArray(LB + lCount)), ByVal VarPtr(tmpS(LB)), lCount * 4
FillMemory ByVal VarPtr(tmpS(LB)), lCount * 2, 0
Note: The LB & UB stuff in sample code is simply to handle any LBound vs. hardcoding for zero
The above is air-code, so apologize in advance if any typos
Edited: You added another post, different data. The above is provided for your original post #1.
Thank you very much
How can I to to duplicate and to triple...etc
I tried
Code:
Dim lCount As Long, UB as Long, LB as Long
LB = LBound(StringArray)
UB = UBound(StringArray)
lCount = Abs(UB - LB) + 1
ReDim Preserve StringArray(LB To LB + lCount * 3 - 1)
For UB = UB + 1 To UB + lCount
StringArray(UB) = StringArray(LB)
LB = LB + 1
Next
But no worked :-(
-
Mar 18th, 2015, 02:23 PM
#6
Re: Duplicate array using copymemory
Code:
Dim lCount As Long, UB as Long, LB as Long
Dim nrCopies As Long
LB = LBound(StringArray)
UB = UBound(StringArray)
lCount = Abs(UB - LB) + 1
nrCopies = 2
ReDim Preserve StringArray(LB To LB + lCount * (nrCopies+1) - 1)
For nrCopies = 0 To nrCopies - 1
For UB = UB + 1 To UB + lCount
StringArray(UB) = StringArray(LB)
LB = LB + 1
Next
LB = LB - lCount
Next
-
Mar 18th, 2015, 02:34 PM
#7
Thread Starter
Fanatic Member
Re: Duplicate array using copymemory
 Originally Posted by LaVolpe
Code:
Dim lCount As Long, UB as Long, LB as Long
Dim nrCopies As Long
LB = LBound(StringArray)
UB = UBound(StringArray)
lCount = Abs(UB - LB) + 1
nrCopies = 2
ReDim Preserve StringArray(LB To LB + lCount * (nrCopies+1) - 1)
For nrCopies = 0 To nrCopies - 1
For UB = UB + 1 To UB + lCount
StringArray(UB) = StringArray(LB)
LB = LB + 1
Next
LB = LB - lCount
Next
It no copy all data
-
Mar 18th, 2015, 02:38 PM
#8
Re: Duplicate array using copymemory
That was air-code and appears I forgot to account for something...
After LB = LB - lCount, add this: UB = UB - 1
-
Mar 18th, 2015, 02:49 PM
#9
Thread Starter
Fanatic Member
Re: Duplicate array using copymemory
 Originally Posted by LaVolpe
That was air-code and appears I forgot to account for something...
After LB = LB - lCount, add this: UB = UB - 1
return error
Subscript out of range
in second interation
-
Mar 18th, 2015, 02:54 PM
#10
Re: Duplicate array using copymemory
I don't have VB on this machine, so I can't troubleshoot it for you.
Just walk it thru and figure out where my math went wrong. In my head, looks like it should work.
Edited: Did follow up with a personal test when I got home. It worked as expected? Did you get it working for you
Last edited by LaVolpe; Mar 19th, 2015 at 08:02 AM.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|