Results 1 to 10 of 10

Thread: Subscript out of range.

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2003
    Posts
    4

    Unhappy Subscript out of range.

    I'm having some troubles with my code. My program reads a file line by line, but each line is delimated with a semicolon. Everything works fine up until the part where it splits each line with the semicolons.

    Code:
      For i = LBound(strArray) To UBound(strArray)
      ReDim Preserve parseplayers(i) As String
      ReDim Preserve players(i) As player
      parseplayers = Split(strArray(i), vbCrLf)
      parseplayers2 = Split(parseplayers(i), ";")
      Next i
    When I try to execute this code, I get a Subscript out of range error and the parseplayers2 = Split(parseplayers(i),";") is highlighted.

    What's wrong? Please help.

  2. #2
    PowerPoster
    Join Date
    Aug 2001
    Location
    new jersey
    Posts
    2,904
    since you didn't bother to show us the declaration for parseplayers2 it is impossible to say for sure but my guess is that you declared it as a string instead of a string array.

  3. #3
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    You haven't ReDimmed 'Players2.

    Also, I can't see how the data integretiy will be maintained in the way your
    redimming too



    Bruce.

  4. #4
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    Don't use the variable i to access the parseplayers array.

    If the statement

    parseplayers = Split(strArray(i), vbCrLf)

    were to create an array of only 2 elements but i was currently equal to 5 the next statement would fail.

  5. #5
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431
    What exactly are you trying to do anyway? Also, I'm thinking you're not quite getting the concept of arrays. I'm thinking you're reading the file into the array strArray (I'm sure you've made it dynamic since you've gotten this far). Then you loop through the array, splitting it into parseplayers2 for some reason (which will not keep its integrity as Bruce said because you keep destroying the data in it from the last iteration). Anyhow, the reason I think you're not getting arrays is because of the parseplayer = Split(strArray(i), vbCrLf) line. I won't go on anymore in case I'm wrong (sure wouldn't be the first time ), but we'd all be glad to help if you could be more specific on what you're doing and using (code-wise along with whatever file you're parsing).

    Finally, two tips: 1, space your code (you may already be doing this, I can't remember whether the [code] [/code] tags preserve it), and a much better tag is the [vbcode] [/vbcode] tag (although it isn't in the help section for some reason)
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  6. #6

    Thread Starter
    New Member
    Join Date
    Sep 2003
    Posts
    4
    Here is all of the code I am using:

    VB Code:
    1. Dim strArray() As String
    2. Dim parseplayers() As String
    3. Dim parseplayers2() As String
    4. Dim i As Integer
    5. Dim i2 As Integer
    6.  
    7. Open "C:\players.txt" For Input As #1
    8. TotalPlayers = 0
    9. Do Until EOF(1) = True
    10. ReDim Preserve strArray(TotalPlayers) As String
    11. Line Input #1, strArray(TotalPlayers)
    12. TotalPlayers = TotalPlayers + 1
    13. Loop
    14. Close #1
    15. Label2.Caption = "Total Players: " & TotalPlayers
    16.  
    17.   For i = LBound(strArray) To UBound(strArray)
    18.   ReDim Preserve parseplayers(i) As String
    19.   ReDim Preserve players(i) As player
    20.   parseplayers = Split(strArray(i), vbCrLf)
    21.   parseplayers2 = Split(parseplayers(i), ";")
    22.  
    23.   For i2 = LBound(parseplayers) To UBound(parseplayers)
    24.             players(i).username = parseplayers2(0)
    25.             players(i).password = parseplayers2(1)
    26.   Next i2
    27.  
    28. Next i

    The data type "player" is custom.

    What I'm trying to do is read a text file of player records, and populate an array (players(i)) with the data.

    Each player record is on a seperate line, and each attribute for teh player record (username and password) is seperated with a semicolon.

  7. #7
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431
    Thanks for clarifying that (I only wish everyone did )

    I redid your indenting (spacing). Usually, you should indent at every block (it's better shown than said). I also got rid of the split. While it would have also worked, for only 2 fields, you can go in using the InStr function and grab them. Here's the code (I've commented it to help explain them better):

    VB Code:
    1. 'This I reconstructed from the info you gave me
    2. Private Type player
    3.     username As String
    4.     password As String
    5. End Type
    6.  
    7. 'I've organized them into types of variables (use whatever the project calls for)
    8. Dim strArray() As String
    9. Dim players() As player
    10.  
    11. Dim TotalPlayers As Single
    12. Dim i As Integer
    13. Dim i2 As Integer
    14.  
    15. 'I put a sub in here (replace it as you need) because I wanted
    16.     'to have you know the difference between the declarations
    17.     'section and a sub's declarations (you may already)
    18. Private Sub Form_Load()
    19. 'I think you know most of what this does, but you didn't quite
    20.     'get the array you put it in (you were splitting at vbCrLf which
    21.     'wouldn't exist in your data anymore)
    22.     'It inputs each LINE into the next array position which is why
    23.     'there are no line breaks.
    24. TotalPlayers = 0
    25. Open "C:\players.txt" For Input As #1
    26.     Do Until EOF(1) = True
    27.         ReDim Preserve strArray(0 To TotalPlayers) As String
    28.         Line Input #1, strArray(TotalPlayers)
    29.         TotalPlayers = TotalPlayers + 1
    30.     Loop
    31. Close #1
    32. Label2.Caption = "Total Players: " & TotalPlayers
    33.  
    34. 'Many ReDims can give you a speed hit. It's better to do it all at
    35.     'once (especially when you know how big it is, ie. For loop
    36.     'instead of, say, Do)
    37. ReDim players(LBound(strArray) To UBound(strArray)) As player
    38. For i = LBound(strArray) To UBound(strArray)
    39.     'Ok, this is the most major change.
    40.     'The first line goes in from the left of the string strArray(i),
    41.         'going InStr(1, strArray(i), ";") - 1 characters right. For example,
    42.         'strArray(i) = "one;two" makes Left("one;two", 4 - 1)
    43.     players(i).username = Left(strArray(i), InStr(1, strArray(i), ";") - 1) '
    44.     'This one is similar, only using the Mid function. Explained as before,
    45.         'this means Go into the string strArray(i) starting at
    46.         'InStr(1, strArray(i), ";") + 1, [going 'till the end]
    47.         'Using the other example, this means Mid("one;two", 5 [, go
    48.         'till the end. This is an optional argument. If not specified,
    49.         'it is assumed you want to go 'till the end of the string])
    50.     players(i).password = Mid(strArray(i), InStr(1, strArray(i), ";") + 1)
    51. Next i
    52. End Sub

    The one thing I didn't explain in there (I felt it was getting cluttered as it is) is the InStr function. InStr(1, strArray(i), ";") means search the string strArray(i), starting at character position 1, for character ";". If it isn't found, it returns zero. I hope this explains what you want. I'd be glad to explain anything else you need.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  8. #8

    Thread Starter
    New Member
    Join Date
    Sep 2003
    Posts
    4
    Thanks alot Jemidiah.

    I didn't quite get the Right, Left, and Mid stuff, but I certainly got the part where you said that I was trying to split up vbCrLf in data that wasn't there.

    I took out the first parseplayers and now it works.

    Thanks.
    -Devon

  9. #9
    Junior Member
    Join Date
    Mar 2000
    Location
    Louisville, KY
    Posts
    21
    Hi "The Devon" !

    I had just a few Suggestions:

    1. Why dont you use Microsoft Scripting Runtime. In it there are Scripting.TextStream and the Scripting.Dictionary Object, which I think will help you with your problem very well.

    2. Quit Using Mid(...), Trim(...), Left(...) and start using Mid$(...), Trim$(...), Left$(...)....unles you want to work with variants.

    These are performance issues that affect the performance of your application.

    Thanks !!!
    Shubhabrata De

  10. #10
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431
    I know I should be using Mid$, etc, but my philosophy when it comes to programming is: if it works, it works (unless, of course, speed is a factor which it may actually be in his code). Oh well, I guess I'll just use it from now on instead of being an old fogie
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

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