|
-
Jan 4th, 2011, 04:54 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Is this the best way for display split array?
Dear all expert programmers,
Please see my code below and suggest me, is this the best way for display split array?
Code:
Option Explicit
Private strTestarray(19) As String '<-- ubound of array can be 32767
Private Sub Command1_Click()
Dim i As Integer
Dim iPart As Integer
Const intMaxpart = 2 '<-- this value can change to other
For i = 0 To 19
strTestarray(i) = Right("0000" & i, 5)
Next
iPart = (UBound(strTestarray) + 1) \ intMaxpart
For i = 1 To iPart
Call DisplaySplitarray(i, intMaxpart)
Next
End Sub
Private Sub DisplaySplitarray(ByVal lngPart As Long, ByVal imax As Integer)
Dim iLoop As Integer
Dim strNewdata() As String
Dim jindex As Long
ReDim strNewdata(imax - 1)
For iLoop = (lngPart - 1) * imax To (lngPart * imax) - 1
strNewdata(jindex) = strTestarray(iLoop)
jindex = jindex + 1
Next
MsgBox lngPart & vbCrLf & Join(strNewdata, vbCrLf) & vbCrLf & "==================" & vbCrLf
End Sub
Thank you for all posts.
-
Jan 4th, 2011, 06:39 PM
#2
Re: Is this the best way for display split array?
I think what you are doing is ok, since there is no inbuilt way in VB6 to get part of an array into another. There could be better/easier/faster ways using API's, so you can investigate about that.
You should specify the type when you ReDim your array, otherwise it will become a variant array (not required in VB.NET). So by specifying the type you should save some cpu cycles, converting from variant and vice-versa.
-
Jan 5th, 2011, 03:23 PM
#3
Re: Is this the best way for display split array?
Here is a small twist:
Code:
Option Explicit
Private Sub Form_Load()
Dim s() As String
Dim i As Long, j As Long
Const PARTS = 2
ReDim s(19)
For i = 0 To 19
s(i) = Right("0000" & i, 5)
Next
MsgBox Join(s, vbNewLine)
For i = 0 To UBound(s) Step PARTS
j = i + PARTS - 1
If j > UBound(s) Then j = UBound(s)
s(i \ PARTS) = s(i)
For j = i + 1 To j
s(i \ PARTS) = s(i \ PARTS) & (vbNewLine & s(j))
Next j
Next i
ReDim Preserve s(i \ PARTS - 1)
MsgBox Join(s, vbNewLine & "---" & vbNewLine)
End Sub
It works with the same array in this case, but what I can see you're very much like wanting to have a join before join.
A more efficient way of doing this would involve hacking the safe array header of the string array for little while, faking it to seem to have far less items and then do a Join for the parts of the array. Join is a pretty good function, performance wise (native Split isn't). But coding it wouldn't be worth it unless there is a need for speed.
-
Jan 13th, 2011, 06:11 AM
#4
Thread Starter
Addicted Member
Re: Is this the best way for display split array?
 Originally Posted by Merri
Here is a small twist:
Code:
Option Explicit
Private Sub Form_Load()
Dim s() As String
Dim i As Long, j As Long
Const PARTS = 2
ReDim s(19)
For i = 0 To 19
s(i) = Right("0000" & i, 5)
Next
MsgBox Join(s, vbNewLine)
For i = 0 To UBound(s) Step PARTS
j = i + PARTS - 1
If j > UBound(s) Then j = UBound(s)
s(i \ PARTS) = s(i)
For j = i + 1 To j
s(i \ PARTS) = s(i \ PARTS) & (vbNewLine & s(j))
Next j
Next i
ReDim Preserve s(i \ PARTS - 1)
MsgBox Join(s, vbNewLine & "---" & vbNewLine)
End Sub
It works with the same array in this case, but what I can see you're very much like wanting to have a join before join.
A more efficient way of doing this would involve hacking the safe array header of the string array for little while, faking it to seem to have far less items and then do a Join for the parts of the array. Join is a pretty good function, performance wise (native Split isn't). But coding it wouldn't be worth it unless there is a need for speed.
Wow, Thank you very much for your code. I like your code too.
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
|