|
-
Sep 28th, 2003, 04:34 PM
#1
Thread Starter
New Member
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.
-
Sep 28th, 2003, 05:30 PM
#2
PowerPoster
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.
-
Sep 28th, 2003, 05:57 PM
#3
You haven't ReDimmed 'Players2.
Also, I can't see how the data integretiy will be maintained in the way your
redimming too 
Bruce.
-
Sep 28th, 2003, 06:19 PM
#4
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.
-
Sep 28th, 2003, 07:15 PM
#5
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.
-
Sep 28th, 2003, 09:36 PM
#6
Thread Starter
New Member
Here is all of the code I am using:
VB Code:
Dim strArray() As String
Dim parseplayers() As String
Dim parseplayers2() As String
Dim i As Integer
Dim i2 As Integer
Open "C:\players.txt" For Input As #1
TotalPlayers = 0
Do Until EOF(1) = True
ReDim Preserve strArray(TotalPlayers) As String
Line Input #1, strArray(TotalPlayers)
TotalPlayers = TotalPlayers + 1
Loop
Close #1
Label2.Caption = "Total Players: " & TotalPlayers
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), ";")
For i2 = LBound(parseplayers) To UBound(parseplayers)
players(i).username = parseplayers2(0)
players(i).password = parseplayers2(1)
Next i2
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.
-
Sep 28th, 2003, 11:37 PM
#7
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:
'This I reconstructed from the info you gave me
Private Type player
username As String
password As String
End Type
'I've organized them into types of variables (use whatever the project calls for)
Dim strArray() As String
Dim players() As player
Dim TotalPlayers As Single
Dim i As Integer
Dim i2 As Integer
'I put a sub in here (replace it as you need) because I wanted
'to have you know the difference between the declarations
'section and a sub's declarations (you may already)
Private Sub Form_Load()
'I think you know most of what this does, but you didn't quite
'get the array you put it in (you were splitting at vbCrLf which
'wouldn't exist in your data anymore)
'It inputs each LINE into the next array position which is why
'there are no line breaks.
TotalPlayers = 0
Open "C:\players.txt" For Input As #1
Do Until EOF(1) = True
ReDim Preserve strArray(0 To TotalPlayers) As String
Line Input #1, strArray(TotalPlayers)
TotalPlayers = TotalPlayers + 1
Loop
Close #1
Label2.Caption = "Total Players: " & TotalPlayers
'Many ReDims can give you a speed hit. It's better to do it all at
'once (especially when you know how big it is, ie. For loop
'instead of, say, Do)
ReDim players(LBound(strArray) To UBound(strArray)) As player
For i = LBound(strArray) To UBound(strArray)
'Ok, this is the most major change.
'The first line goes in from the left of the string strArray(i),
'going InStr(1, strArray(i), ";") - 1 characters right. For example,
'strArray(i) = "one;two" makes Left("one;two", 4 - 1)
players(i).username = Left(strArray(i), InStr(1, strArray(i), ";") - 1) '
'This one is similar, only using the Mid function. Explained as before,
'this means Go into the string strArray(i) starting at
'InStr(1, strArray(i), ";") + 1, [going 'till the end]
'Using the other example, this means Mid("one;two", 5 [, go
'till the end. This is an optional argument. If not specified,
'it is assumed you want to go 'till the end of the string])
players(i).password = Mid(strArray(i), InStr(1, strArray(i), ";") + 1)
Next i
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.
-
Sep 29th, 2003, 10:48 AM
#8
Thread Starter
New Member
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
-
Sep 29th, 2003, 11:13 AM
#9
Junior Member
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 !!!
-
Sep 29th, 2003, 04:48 PM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|