|
-
Feb 12th, 2009, 12:29 PM
#1
Thread Starter
New Member
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.
-
Feb 12th, 2009, 02:45 PM
#2
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 
-
Feb 13th, 2009, 03:45 AM
#3
Thread Starter
New Member
Re: Add new element and rearrange an array
-
Feb 13th, 2009, 05:57 AM
#4
Re: Add new element and rearrange an array
here is another method
vb Code:
Sub addto(ByRef sarr() As String, narr() As String, pos As Integer) tmp = sarr j = UBound(narr) ReDim sarr(UBound(sarr) + j + 1) For i = 0 To UBound(narr) sarr(i) = narr(i) Next For i = pos To UBound(tmp) j = j + 1 sarr(j) = tmp(i) Next For i = 0 To pos - 1 j = j + 1 sarr(j) = tmp(i) Next End Sub
call like
vb Code:
Dim strarr() As String, addarr() As String mstr = "1.1 ; - 2.1 ; 3.1 ; - 1.0 ; 3.0 ; 5.1 ; 0.2 ; 0.5" strarr = Split(mstr, ";") addarr = Split("1.1; 0.1", ";") 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
-
Feb 13th, 2009, 08:13 AM
#5
Thread Starter
New Member
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.
-
Feb 13th, 2009, 08:28 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|