Results 1 to 4 of 4

Thread: [RESOLVED] Is this the best way for display split array?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2007
    Posts
    227

    Resolved [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.

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    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.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2007
    Posts
    227

    Re: Is this the best way for display split array?

    Quote Originally Posted by Merri View Post
    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
  •  



Click Here to Expand Forum to Full Width