Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating Arrays - HELP reading/writing from/to text files

Status
Not open for further replies.

londonkiwi

Programmer
Sep 25, 2000
83
NZ
Can anyone put me on the right direction for the structure and approach, perhaps code??

HH:MM:SS DD-MM-YY DR SPEED WBASE HDWY AX G CL TYP WHEEL-PICTURE
13:01:12 04-10-00 AB 71.1 2.6 58.7 2 2 1 SV o o
13:09:05 04-10-00 BA 91.3 2.8 >1 hr 2 2 1 SV o o
13:09:43 04-10-00 BA 57.7 2.6 37.7 2 2 1 SV o o

08:54:37 05-10-00 BA 68.8 6.1 535.9 3 3 2 SVT o o o
09:05:12 05-10-00 BA 73.8 2.7 634.8 2 2 1 SV o o etc

I need to: Group times into the following periods (hours), 0-6, 7-12, 13-18, 19-24 (ie 0,6,12,18,24), for each day. The column CL can have values 1-13. The column DR (direction) can be AB (1) or BA (2). For each day, I need to count the number of instances of each kind of CL values for each direction (AB or BA) and time period, and write them to a file.

Something like this: (filename,date, time period,DR, Total of CL (1-13) values for that period eg, CL 1 could have occurred 20 times

kiwi42,04-10-00,0,1,0001,0024,0000,0002,0200,0023,0001,0000,0450,0000,0003,0005,0001
kiwi42,04-10-00,0,2,0001,0023,0000,0002,0100,0023,0001,0000,0430,0000,0003,0005,0001
kiwi42,04-10-00,6,1,0001,0023,0000,0002,0100,0023,0001,0000,0430,0000,0003,0005,0001
kiwi42,04-10-00,12,2,0001,0023,0000,0002,0100,0023,0001,0000,0430,0000,0003,0005,0001
kiwi42,05-10-00,18,1,0001,0023,0000,0002,0100,0023,0001,0000,0430,0000,0003,0005,0001

I realise that reading this data into an array would be the smartest thing to do. I have started by trying to compared two dates, but got lost in the nesting of my arrays.


cheers lk [sig][/sig]
 
Well the basics of opening a text file are

' Read in data
Open "C:\Filename" For Input As #1
Do Until EOF(1)
Line Input #1, A
Debug.Print A
Loop
Close #1


' to write out a file similar metthod except use Output in stead of Input
Open "C:\Filename" For Output As #1
Do Until EOF(1)
' but here you need to generate the data to write out and put it in 'A'
Print #1, A
Loop
Close #1
[sig]<p>DougP, MCP<br><a href=mailto: dposton@universal1.com> dposton@universal1.com</a><br><a href= > </a><br> Ask me how Bar-codes can help you be more productive.[/sig]
 
The following will read in the (sample) data and ?? convert ?? it to a useable format. Liberties taken in the 're-arrangement' of the time/date format and the 'HDWY' variable, so look at these and be sure they will 'work' for you. This generates a single array of a UDT (User Defined Type), which should help in dealing with the multiple array indicies.


Option Compare Database
Option Explicit


' 1 2 3 4 5 6
'1234567890123456879012345678901234568790123456789012345687901234
'HH:MM:SS DD-MM-YY DR SPEED WBASE HDWY AX G CL TYP WHEEL-PICTURE
'13:01:12 04-10-00 AB 71.1 2.6 58.7 2 2 1 SV o o
'13:09:05 04-10-00 BA 91.3 2.8 >1 hr 2 2 1 SV o o
'13:09:43 04-10-00 BA 57.7 2.6 37.7 2 2 1 SV o o


Private Type MyDataType
MyDate As Date
DirCt As String * 2
Speed As Single
WhBase As Single
Hdwy As Single
Ax As Integer
G As Integer
Cl As Integer
Typ As String * 2
Wheel As String * 1
Pict As String * 1
End Type
Dim MyData() As MyDataType


Public Function basReadTestData()

'Function to read/convert test data

Dim MyFil As Integer
Dim Idx As Integer
Dim MyLine As String
Dim MyTmp As String

Idx = 0
ReDim MyData(Idx) 'Initalize array to one element

MyFil = FreeFile
Open &quot;C:\My Documents\TestData.Txt&quot; For Input As #MyFil

While Not EOF(MyFil)
Line Input #MyFil, MyLine
MyLine = Trim(MyLine)

MyData(Idx).MyDate = Mid(MyLine, 10, 8) & &quot; &quot; & Left(MyLine, 8)
MyData(Idx).DirCt = Trim(Mid(MyLine, 18, 3))
MyData(Idx).Speed = CSng(Mid(MyLine, 21, 6))
MyData(Idx).WhBase = CSng(Mid(MyLine, 27, 6))

MyTmp = Mid(MyLine, 34, 5)
If (IsNumeric(MyTmp)) Then
MyData(Idx).Hdwy = CSng(MyTmp)
Else
MyData(Idx).Hdwy = 60
End If

MyData(Idx).Ax = CInt(Mid(MyLine, 39, 3))
MyData(Idx).G = CInt(Mid(MyLine, 42, 2))
MyData(Idx).Cl = CInt(Mid(MyLine, 44, 3))
MyData(Idx).Typ = Trim(Mid(MyLine, 48, 3))
MyData(Idx).Wheel = Trim(Mid(MyLine, 51, 3))
MyData(Idx).Pict = Trim(Mid(MyLine, 54, 3))

Idx = Idx + 1
ReDim Preserve MyData(Idx)

Wend

Close #MyFil 'Close Input
ReDim Preserve MyData(Idx - 1) 'remove last (Unused) array element


For Idx = 0 To UBound(MyData)
With MyData(Idx)
Debug.Print .MyDate, .DirCt, .Speed, .WhBase, .Hdwy, .Ax, .G, .Cl, .Typ, .Wheel, .Pict
End With
Next Idx


End Function

[sig]<p>MichaelRed<br><a href=mailto:mred@duvallgroup.com>mred@duvallgroup.com</a><br>There is never time to do it right but there is always time to do it over[/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top