|
-
Feb 12th, 2009, 11:16 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Adding to array
If I have a bunch of these in a textfile:
Code:
[Event "abc 1841"]
[Date "1841-xx-xx"]
[Black "x"]
[White "Y"]
[Site "Z"]
[Result "0-1"]
x1x2
[Event "abc 1841"]
[Date "1841-xx-xx"]
[Black "b"]
[White "r"]
[Site "Z"]
[Result "0-1"]
x122
[Event "def 1842"]
[Date "1842-xx-??"]
[Black "X"]
[White "Y"]
[Site "Z"]
[Result "1/2-1/2"]
z1z3
how do i take the chunks of info, separate them by year, and put each chunk (all 7 lines) into an array?
So in the example above, in the 1841 array, I would have:
Code:
Array1(0) = [Event "abc 1841"]
[Date "1841-xx-xx"]
[Black "x"]
[White "Y"]
[Site "Z"]
[Result "0-1"]
x1x2
Array1(1) = [Event "abc 1841"]
[Date "1841-xx-xx"]
[Black "b"]
[White "r"]
[Site "Z"]
[Result "0-1"]
x122
and the 1842 array would have:
Code:
Array2(0) = [Event "def 1842"]
[Date "1842-xx-??"]
[Black "X"]
[White "Y"]
[Site "Z"]
[Result "1/2-1/2"]
z1z3
The date could be anywhere from 1841 to 2004. The date will always be the last 4 chars at the end of "Date" (2nd line).
Thanks!
"Remember, remember the 5th of November, the gun powder treason and plot. I know of no reason why the gun powder treason should ever be forgot."
-
Feb 13th, 2009, 12:27 AM
#2
Addicted Member
Re: Adding to array
Hai,
ou can search the text '[date' and concatenate the data and then find out the year, based on that year you can store the data in that array.
-
Feb 13th, 2009, 03:30 AM
#3
Re: Adding to array
a bit hard to put into separate arrays, easier to put into a single 2d array
vb Code:
Dim somearray() As String, filearray() As String Open "c:\test\kozz.txt" For Input As 1 filearray = Split(Input(LOF(1), #1), vbNewLine) Close 1 ReDim somearray(UBound(filearray) \ 8, 6) 'set the size of the array based on the number of lines in file x 7 elements For j = 0 To UBound(filearray) Step 8 'loop through all lines from file For i = 0 To 6 somearray(j / 8, i) = filearray(j + i) Next Next
somearray now contains all the lines in separate elements
somearray(0, 0) = [Event "abc 1841"]
somearray(0, 1) = [Date "1841-xx-xx"]
somearray(0, 2) = [Black "x"]
somearray(0, 3) = [White "Y"]
somearray(0, 4) = [Site "Z"]
somearray(0, 5) = [Result "0-1"]
somearray(0, 6) = x1x2
somearray(1, 0) = [Event "abc 1841"]
somearray(1, 1) = [Date "1841-xx-xx"]
somearray(1, 2) = [Black "b"]
somearray(1, 3) = [White "r"]
somearray(1, 4) = [Site "Z"]
somearray(1, 5) = [Result "0-1"]
somearray(1, 6) = x122
somearray(2, 0) = [Event "def 1842"]
somearray(2, 1) = [Date "1842-xx-??"]
somearray(2, 2) = [Black "X"]
somearray(2, 3) = [White "Y"]
somearray(2, 4) = [Site "Z"]
somearray(2, 5) = [Result "1/2-1/2"]
somearray(2, 6) = z1z3
Last edited by westconn1; Feb 13th, 2009 at 03:37 AM.
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Feb 13th, 2009, 06:51 AM
#4
Re: Adding to array
Try this:
Code:
Option Explicit
Type YEAR_REC
Year As Integer
Recs() As String
End Type
Dim YRecs() As YEAR_REC
Code:
Function GetData() As String
'-- replace with your code to read data from file
GetData = "[Event ""abc 1841""]"
GetData = GetData & vbCrLf & "[Date ""1841-xx-xx""]"
GetData = GetData & vbCrLf & "[Black ""X""]"
GetData = GetData & vbCrLf & "[White ""Y""]"
GetData = GetData & vbCrLf & "[Site ""Z""]"
GetData = GetData & vbCrLf & "[Result ""0-1""]"
GetData = GetData & vbCrLf & "x1x2"
GetData = GetData & vbCrLf & ""
GetData = GetData & vbCrLf & "[Event ""abc 1841""]"
GetData = GetData & vbCrLf & "[Date ""1841-xx-xx""]"
GetData = GetData & vbCrLf & "[Black ""b""]"
GetData = GetData & vbCrLf & "[White ""r""]"
GetData = GetData & vbCrLf & "[Site ""Z""]"
GetData = GetData & vbCrLf & "[Result ""0-1""]"
GetData = GetData & vbCrLf & "x122"
GetData = GetData & vbCrLf & ""
GetData = GetData & vbCrLf & "[Event ""def 1842""]"
GetData = GetData & vbCrLf & "[Date ""1842-xx-??""]"
GetData = GetData & vbCrLf & "[Black ""X""]"
GetData = GetData & vbCrLf & "[White ""Y""]"
GetData = GetData & vbCrLf & "[Site ""Z""]"
GetData = GetData & vbCrLf & "[Result ""1/2-1/2""]"
GetData = GetData & vbCrLf & "z1z3"
End Function
Code:
Sub GetYearRecs()
Dim sData As String
Dim i As Long, n As Long, y As Integer
Dim sRecs() As String
Dim YCount(2004 - 1841) As Long
ReDim YRecs(2004 - 1841)
sData = GetData()
sRecs = Split(sData, vbCrLf & vbCrLf)
For i = 0 To UBound(sRecs)
n = InStr(sRecs(i), "[Date """)
y = Mid(sRecs(i), n + 7, 4) - 1841
ReDim Preserve YRecs(y).Recs(YCount(y))
YRecs(y).Recs(YCount(y)) = sRecs(i)
YCount(y) = YCount(y) + 1
Next
n = -1
For y = 0 To 2004 - 1841
If YCount(y) > 0 Then
n = n + 1
YRecs(y).Year = y + 1841
If n < y Then YRecs(n) = YRecs(y)
End If
Next
ReDim Preserve YRecs(n)
For y = 0 To n
Debug.Print "+++ YEAR:"; YRecs(y).Year
Debug.Print "-------------------"
For i = 0 To UBound(YRecs(y).Recs)
Debug.Print YRecs(y).Recs(i)
Debug.Print "-------------------"
Next
Next
End Sub
-
Feb 14th, 2009, 04:34 PM
#5
Thread Starter
Hyperactive Member
Re: Adding to array
Anhn, every time I use the code you gave me,
It fails to recognize the two new lines. When I made the code read from a file containing data (and assigned it to GetData) using data such as this:
Code:
[Event "London 1854, game 4"]
[Date "1854-??-??"]
[Black "Martins, R."]
[White "Brown, A."]
[Site "London"]
[Result "1/2-1/2"]
[Round "4"]
1. 11-15 23-19 2. 8-11 22-17 3. 9-13 17-14 4. 10x17 21x14 5. 15-18 26-23 6.
13-17 19-15 7. 4-8 24-19 8. 6-9 28-24 9. 9-13 25-21 10. 2-6 30-25 11. 17-22
21-17 12. 6-9 24-20 13. 1-6 32-28 14. 12-16 19x12 15. 7-10 14x7 16. 3x10x19x26
12x3 17. 26-30 25-21 18. 11-15 3-7 19. 22-26 31x22 20. 18x25 29x22 21. 6-10 7x14
22. 9x18x25 17-14 23. 30-26 14-10 24. 26-22 27-23 25. 25-30 10-7 26. 30-26 23-19
27. 15x24 28x19 28. 22-18 7-3 29. 18-14 3-7 30. 13-17 19-16 31. 17-22 16-12 32.
22-25 20-16 33. 25-30 12-8 34. 5-9 8-3 35. 14-18 16-12 36. 9-14 12-8 37. 18-22
8-4 38. 14-18 7-11 39. 18-23 3-8 40. 23-27 11-16 41. 27-32 8-12 42. 32-28 4-8 1/2-1/2
[Event "London 1854, game 5"]
[Date "1854-??-??"]
[Black "Brown, A."]
[White "Martins, R."]
[Site "London"]
[Result "1/2-1/2"]
[Round "5"]
1. 11-15 23-19 2. 8-11 22-17 3. 9-14 25-22 4. 11-16 24-20 5. 16x23 27x18x11 6.
7x16 20x11 7. 3-7 28-24 8. 7x16 24-19 9. 16x23 26x19 10. 4-8 30-26 11. 8-11
26-23 12. 11-15 32-28 13. 15x24 28x19 14. 5-9 17-13 15. 10-15 19x10 16. 6x15
13x6 17. 1x10 22-17 18. 12-16 29-25 19. 15-18 17-13 20. 18x27 31x24 21. 10-15
24-20 22. 16-19 25-22 23. 14-18 22-17 24. 19-24 17-14 25. 15-19 14-10 26. 18-23
20-16 27. 23-26 16-11 28. 26-30 13-9 29. 24-27 21-17 30. 27-31 17-13 1/2-1/2
[Event "Newcastle 1858, game 6"]
[Date "1858-??-??"]
[Black "Way, J."]
[White "Coltherd, H."]
[Site "Newcastle"]
[Result "1-0"]
[Round "6"]
1. 11-15 23-19 2. 8-11 22-17 3. 3-8 25-22 4. 11-16 26-23 5. 16-20 29-25 6. 7-11
31-26 7. 9-14 19-16 8. 12x19 23x16x7 9. 2x11 26-23 10. 8-12 17-13 11. 4-8 23-19
12. 15-18 22x15 13. 11x18 30-26 14. 10-15 19x10 15. 6x15 13-9 16. 8-11 26-22 17.
18-23 27x18 18. 14x23 24-19 19. 15x24 28x19 20. 5x14 22-17 21. 14-18 19-15 22.
11-16 17-13 23. 23-26 1-0
It refused to recognize the double line break in between the chunks of data - I could tell because when I looked at the upperbound of sRecs, it was 0. I even tried to see if it would seperate just one line break - but even then, the upperbound of sRecs remained zero.
Why doesn't this work? Thanks for your time.
"Remember, remember the 5th of November, the gun powder treason and plot. I know of no reason why the gun powder treason should ever be forgot."
-
Feb 14th, 2009, 05:10 PM
#6
Re: Adding to array
How do you read your data file? Show your code.
Code:
Function GetData() As String
Open "C:\xyz\Data.txt" For Input As #1
GetData = Input(LOF(1), 1)
Close #1
End Function
If the GetData() above gives the same problem then:
The code only works if data file uses vbCrLf as line break.
In some cases vbLf is used as line break.
You can test what is line break character like this:
Code:
sData = GetData()
If Instr(sData, vbCrLf) Then
sRecs = Split(sData, vbCrLf & vbCrLf)
Else
sRecs = Split(sData, vbLf & vbLf)
End If
-
Feb 14th, 2009, 08:47 PM
#7
Thread Starter
Hyperactive Member
Re: Adding to array
Thanks! Working great now. I appreciate it.
"Remember, remember the 5th of November, the gun powder treason and plot. I know of no reason why the gun powder treason should ever be forgot."
-
Mar 4th, 2009, 11:06 PM
#8
Thread Starter
Hyperactive Member
Re: [RESOLVED] Adding to array
If my text file included entries like this:
[Event "US National 1956"]
[Black "H Anderson"]
[White "MF Tinsley"]
[Result "1/2-1/2"]
xxxxxx
How would I make it work?
This time the date is the last few characters of the "event" row.
Thanks.
"Remember, remember the 5th of November, the gun powder treason and plot. I know of no reason why the gun powder treason should ever be forgot."
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
|