PDA

Click to See Complete Forum and Search --> : [VB] Data Format


run_GMoney
Jan 23rd, 2004, 10:45 AM
I've included 2 text files with this project. The first (Data.txt) is my input data set and the second is how my outputted data is supposed to appear. Here's is the code that I've used to input the data, format it, and output it. My code produces the correct output but I'm sure there's a more optimal way to do this.


Private Function GetData()

Open "F:\Acell\Data.txt" For Input As #1
lineArray = Split(Input(LOF(1), 1), vbCrLf)
Close #1

'Stores the data into an array and trims any leading spaces
For i = 1 To (UBound(lineArray) - 2)
T(i - 1) = FormatNumber(LTrim(Mid(lineArray(i), 1, 8)), 3)
ax0(i - 1) = FormatNumber(LTrim(Mid(lineArray(i), 9, 10)), 3)
ax1(i - 1) = FormatNumber(LTrim(Mid(lineArray(i), 19, 10)), 3)
ax3(i - 1) = FormatNumber(LTrim(Mid(lineArray(i), 29, 10)), 3)
ay0(i - 1) = FormatNumber(LTrim(Mid(lineArray(i), 39, 10)), 3)
ay2(i - 1) = FormatNumber(LTrim(Mid(lineArray(i), 49, 10)), 3)
ay3(i - 1) = FormatNumber(LTrim(Mid(lineArray(i), 59, 10)), 3)
az0(i - 1) = FormatNumber(LTrim(Mid(lineArray(i), 69, 10)), 3)
az2(i - 1) = FormatNumber(LTrim(Mid(lineArray(i), 79, 10)), 3)
az1(i - 1) = FormatNumber(LTrim(Mid(lineArray(i), 89, 10)), 3)
Next

End Function

Private Function WriteFile2()

Open App.Path & "\RetrieveData.dat" For Output As #1
Print #1, "Time(ms) " & Space(3) & "HD-OX" & Space(5) & "HD-YX" & Space(5) & "HD-ZX" & _
Space(5) & "HD-OY" & Space(5) & "HD-XY" & Space(5) & "HD-ZY" & Space(5) & _
"HD-OZ" & Space(5) & "HD-XZ" & Space(5) & "HD-YZ"
Close #1

Open App.Path & "\RetrieveData.dat" For Append As #1
For i = 1 To (UBound(lineArray) - 2)
Call computeSpaces(ax0(i))
ax0spaces = numspaces
Call computeSpaces(ax1(i))
ax1spaces = numspaces
Call computeSpaces(ax3(i))
ax3spaces = numspaces
Call computeSpaces(ay0(i))
ay0spaces = numspaces
Call computeSpaces(ay2(i))
ay2spaces = numspaces
Call computeSpaces(ay3(i))
ay3spaces = numspaces
Call computeSpaces(az0(i))
az0spaces = numspaces
Call computeSpaces(az2(i))
az2spaces = numspaces
Call computeSpaces(az1(i))
az1spaces = numspaces
Print #1, T(i) & Space(ax0spaces) & ax0(i) & Space(ax1spaces) & ax1(i) & _
Space(ax3spaces) & ax3(i) & Space(ay0spaces) & ay0(i) & Space(ay2spaces) & ay2(i) & _
Space(ay3spaces) & ay3(i) & Space(az0spaces) & az0(i) & Space(az2spaces) & az2(i) & _
Space(az1spaces) & az1(i)
Next
Close #1

End Function

Private Sub computeSpaces(datapoint)

If datapoint <= -1000 Then
numspaces = 1
ElseIf datapoint <= -100 Then
numspaces = 2
ElseIf datapoint <= -10 Then
numspaces = 3
ElseIf datapoint < 0 Then
numspaces = 4
ElseIf datapoint < 10 Then
numspaces = 5
ElseIf datapoint < 100 Then
numspaces = 4
ElseIf datapoint < 1000 Then
numspaces = 3
ElseIf datapoint < 10000 Then
numspaces = 2
ElseIf datapoint < 100000 Then
numspaces = 1
End If

End Sub