Results 1 to 8 of 8

Thread: [RESOLVED] How to split an array into several arrays?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2008
    Posts
    136

    Resolved [RESOLVED] How to split an array into several arrays?

    Hi everyone,

    i really need some help on this situation. i load a text file which contains several lines (items), let's say 231 items, into an array named strMainItems. After that i want to split that strMainItems into several arrays named strItemA, strItemB and so on which contains 100 items per array. so with my code the result will like this:

    strMainItems = 0 - 230
    strItemA = 0 - 99
    strItemB = 100 - 199
    strItemC = 200 - 230

    but i don't want them to be like that. i want each array has an index start from 0, like this:

    strItemA = 0 - 99
    strItemB = 0 - 99
    strItemC = 0 - 30

    how can i do that? and what if the text file contains bigger items, say like 531. if so, then i have to make as many variable as needed (strItemA, strItemB, strItemC, strItemD, strItemE, strItemF til enough) manually..

    this is my code:

    Code:
    Option Explicit
    
    Dim strItemA(0 To 99) As String
    Dim strItemB(100 To 199) As String
    Dim strItemC(200 To 230) As String
    
    Private Sub Command1_Click()
        Dim iFile As Integer
        Dim strMainItems() As String
        Dim i As Long
        iFile = FreeFile
        Open "friends.txt" For Input As iFile
        strMainItems = Split(Input$(LOF(iFile), iFile), vbCrLf)
        
        For i = 0 To 99
            strItemA(i) = strMainItems(i)
        Next i
        
        For i = 100 To 199
            strItemB(i) = strMainItems(i)
        Next i
    
        For i = 200 To 230
            strItemC(i) = strMainItems(i)
        Next i
        
        Close iFile
    End Sub
    and i guess the idea to load a text file first into strMainItems is not good if it can be done by directly load those items into strItemA, strItemB, strItemC and so on.. but i don't know how..

    thank you very much..

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How to split an array into several arrays?

    Easy, since you are using separate arrays, each can have the index's the way you want. Just Dimension them all the same.

    Code:
    Dim strItemA(0 To 99) As String
    Dim strItemB(0 To 99) As String
    Dim strItemC(0 To 30) As String
    Now when your looping, dont add the "i" as its index or it will be out of bounds. Calculate the subtraction so it equates the base to start adding from.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2008
    Posts
    136

    Re: How to split an array into several arrays?

    hi rob, thank you for your reply. i modified my code like this:

    Code:
    Option Explicit
    
    Dim strItemA(0 To 99) As String
    Dim strItemB(0 To 99) As String
    Dim strItemC(0 To 30) As String
    
    Private Sub Command1_Click()
        Dim iFile As Integer
        Dim strMainItems() As String
        Dim i As Long
        Dim x As Long
        iFile = FreeFile
        Open "friends.txt" For Input As iFile
        strMainItems = Split(Input$(LOF(iFile), iFile), vbCrLf)
        
        For i = 0 To 99
            strItemA(x) = strMainItems(i)
            x = x + 1
        Next i
        
        x = 0
        
        For i = 100 To 199
            strItemB(x) = strMainItems(i)
            x = x + 1
        Next i
        
        x = 0
    
        For i = 200 To 230
            strItemC(x) = strMainItems(i)
            x = x + 1
        Next i
        
        Close iFile
    End Sub
    it works. do you mean like that?

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How to split an array into several arrays?

    Yea but was thinking more like this for the second loop and third. No need for an extra counter variable.
    Code:
        For i = 100 To 199
            strItemB(i - 100) = strMainItems(i)
        Next i
    
        For i = 200 To 230
            strItemC(i - 200) = strMainItems(i)
        Next i
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2008
    Posts
    136

    Re: How to split an array into several arrays?

    thx again rob, i use now your code coz it doesn't need an extra counter variable..
    my first question has got an answer..

    now, can anyone show me how to do for my 2nd question?
    i want those variables (strItemA, strItemB and so on) will be created on runtime depends on how many items exist in strMainItems. so if strMaintems contains 531 items then i don't have to define 6 variables manually, but they will be created automatically..

    thx..

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

    Re: How to split an array into several arrays?

    I think you shouldn't be doing that in the first place. Instead you could create a function that gives you what you want:

    Code:
    Private strMainItems() As String
    
    Public Property Get Item(ByVal ID As Byte, ByVal Index As Byte) As String
        Dim lngIndex As Long
        ' calculate index
        lngIndex = ID * 100& + Index
        ' return string if exists
        If lngIndex < UBound(strMainItems) Then Item = strMainItems(lngIndex)
    End Function
    
    Public Property Let Item(ByVal ID As Byte, ByVal Index As Byte, ByRef NewValue As String)
        Dim lngIndex As Long
        ' calculate index
        lngIndex = ID * 100& + Index
        ' change string if exists
        If lngIndex < UBound(strMainItems) Then strMainItems(lngIndex) = NewValue
    End Property
    Instead of strItemA(0...99) you now use Item(0, 0...99)
    Instead of strItemB(0...99) you now use Item(1, 0...99)

    You can get the maximum ID value by doing MaxID = UBound(strMainItems) \ 100
    You can get the maximum index of the last ID with MaxIndex = UBound(strMainItems) Mod 100

  7. #7
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: How to split an array into several arrays?

    Another approach: use the 3rd parameter of the Split() function to stop splitting at item 101.
    Code:
    Option Explicit
    
    Type StringList
        Item() As String
    End Type
    Dim List100() As StringList
    
    Private Sub Command1_Click()
        Dim iFile As Integer
        Dim sData As String
        Dim i     As Long
        
        iFile = FreeFile
        Open "C:\MyFolder\friends.txt" For Input As #iFile
        sData = Input$(LOF(iFile), iFile)
        Close #iFile
        i = 0
        Do
            ReDim Preserve List100(i)
            List100(i).Item = Split(sData, vbCrLf, 101)   '-- split sData to maximun 101 items
            If UBound(List100(i).Item) < 100 Then Exit Do '-- if less than 101 items then done
            sData = List100(i).Item(100)                  '-- set sData to the last item (101st)
            ReDim Preserve List100(i).Item(0 To 99)    '-- remove the last item from List100(i)
            i = i + 1 '-- next List100
        Loop
    End Sub
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Aug 2008
    Posts
    136

    Re: How to split an array into several arrays?

    Hi Merri and anhn,

    thank you for your help. both of your codes work properly and exactly as i want..
    but with Merri's code it doesn't load the last item. if the last item in text file is "Item231" then it will load "Item230". to resolve this i just add 1 blank line at the end of the text file, and subtract by 1 of the maximum index of last ID. perhaps i can think another way to resolve this, but at this moment, this helps me much..

    Merri's code:
    Code:
    MsgBox "Last item in last array is : " & Item(2, (UBound(strMainItems) Mod 100) - 1)
    text file:
    Code:
    Item1
    ...
    ...
    Item230
    Item231
    -----blank line-----

    anhn's code:
    Code:
    MsgBox "Last item in last array is : " & List100(2).Item(UBound(List100(2).Item))
    text file:
    text file:
    Code:
    Item1
    ...
    ...
    Item230
    Item231
    thank you once again..

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