I have attached that file. The sample of data are as below:
PUBLT 167_89 656 PB -38321.478 6807.984 657 PB DAUD 200005041 278.271074.5150 69.4035 08370074.1830 69.2755 083800254.1925290.4620083900254.5615291.010008400008390098.2710 74.3630 20.3943 278.27100.0211 15.5954 74.3726 278.2748
PUBLT 167_89 656 PB -38321.478 6807.984 657 PB DAUD 200005042 278.271074.4940 68.1425 08430074.1610 68.0010 084400254.1720292.1650084500254.5400292.315508460008450098.2710 74.3418 22.0833 278.27100.0201 15.5958 74.3511 278.2746

what i gonna want to do is to extract those red data and then some simple calculation will be added because there are 2 lines for the same line number such as data for 656-657. The calculation are to get the average from the two values. The sample output are as below:

-this is data extracted for each line
656-657 278.2748
656-657 278.2746
-the final output i want is the average of those two line
656-657 278-27-47

For the calculation, the formula is as below:
278.2748 and 278.2746 are the value for bearing/ azimuth. It means 278 degree 27 minutes 48 seconds. So to get the average value of those two, we must consider the degree, minutes and seconds.
1. convert degree minutes seconds to decimal degrees
278+(27/60)+(48/3600) >>>> result is 278.4633333
278+(27/60)+(46/3600) >>>> result is 278.4627778

278.4633333 x 180 / PI >>>>result is 4.860102013
278.4627778 x 180 / PI >>>>result is 4.860092316

3. get average
(4.860102013 + 4.860092316) / 2 >>>result is 4.860097165

4. Convert back average radian to degrees
4.860097165 x PI / 180 >>>>>>> result is 278.4630556

4. convert back degrees to degree minutes seconds
(for this formula i will create myself...)

the main problem here is i want to use 2 value from 2 line in one time to do that calculation... if those data in one line its not a problem.. how can i possibly do that?

Use ReadAllLines method of File type.

open the file line input split the line with space delimeter put the data in the array then manipulate the array number.

Your file looks like a fixed-length file rather than space-delimited so I would not recommend using `Split()` but rather reading the positions. This is almost identical to your last thread except you're wanting to perform calculations. Copy the code from your last thread to read each segment of the …

## All 10 Replies

Use ReadAllLines method of File type.

open the file line input split the line with space delimeter put the data in the array then manipulate the array number.

PUBLT 167_89 656 PB -38321.478 6807.984 657 PB DAUD 200005041 278.271074.5150 69.4035 08370074.1830 69.2755 083800254.1925290.4620083900254.5615291.010008400008390098.2710 74.3630 20.3943 278.27100.0211 15.5954 74.3726 278.2748
PUBLT 167_89 656 PB -38321.478 6807.984 657 PB DAUD 200005042 278.271074.4940 68.1425 08430074.1610 68.0010 084400254.1720292.1650084500254.5400292.315508460008450098.2710 74.3418 22.0833 278.27100.0201 15.5958 74.3511 278.2746

what i gonna want to do is to extract those red data and then some simple calculation will be added because there are 2 lines for the same line number such as data for 656-657. The calculation are to get the average from the two values. The sample output are as below:

-this is data extracted for each line
656-657 278.2748
656-657 278.2746
-the final output i want is the average of those two line
656-657 278-27-47

Your file looks like a fixed-length file rather than space-delimited so I would not recommend using `Split()` but rather reading the positions. This is almost identical to your last thread except you're wanting to perform calculations. Copy the code from your last thread to read each segment of the line to local variables.

Are the two lines guaranteed to always be in order? If they are then you can "peek" ahead a single line to get the second set of values. If they are sometimes out of order you would probably want to digest the entire file in memory so you can look up other line numbers.

Give this a shot and post some code and we'll work from there. Also when you post test data you should use code tags to preserve formatting which is very important for text file parsing. If you're posting sample data use `[code=text]`:

``````PUBLT     167_89              656       PB                  -38321.478  6807.984    657       PB                  DAUD                                                                                                200005041 278.271074.5150 69.4035 08370074.1830 69.2755 083800254.1925290.4620083900254.5615291.010008400008390098.2710 74.3630 20.3943 278.27100.0211  15.5954 74.3726 278.2748
PUBLT     167_89              656       PB                  -38321.478  6807.984    657       PB                  DAUD                                                                                                200005042 278.271074.4940 68.1425 08430074.1610 68.0010 084400254.1720292.1650084500254.5400292.315508460008450098.2710 74.3418 22.0833 278.27100.0201  15.5958 74.3511 278.2746
PUBLT     167_89              348       Pkt                 -41274.383  5533.260    349       PB                  DAUD                                                                                                200002231 269.1220104.032063.1220 084800103.341062.5710 084800283.3740297.1550084900284.1330297.322008490008490089.1220 103.521027.0940 269.12200.0135  -10.0855103.5150269.1146
PUBLT     167_89              348       Pkt                 -41274.383  5533.260    349       PB                  DAUD                                                                                                200002232 269.1220104.205061.4620 085400103.521061.3220 085400283.5920298.5700085500284.3510299.112008550008550089.1220 104.115328.4225 269.12200.0128  -10.0850104.1141269.1154
``````
``````Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Const inFile As String = "C:\sob.txt"
Const outFile As String = "C:\sob_new.txt"
Using sw As New System.IO.StreamWriter(outFile, False)
Dim line As String
Do
If Not String.IsNullOrEmpty(line) Then
Dim fields(5) As String
fields(0) = line.Substring(30, 5).Trim()
fields(1) = line.Substring(84, 5).Trim()
Dim chunk As String() = line.Substring(382, 8).Split(".")
fields(2) = chunk(0).Trim()
fields(3) = chunk(1).Substring(0, 2).Trim()
fields(4) = chunk(1).Substring(2, 2).Trim()
sw.WriteLine(String.Format("{0}-{1} {2}-{3}-{4}", fields))

End If
Loop Until line Is Nothing
sw.Flush()
sw.Close()
End Using
End Using

End Sub
End Class``````

This is my code... i dont have an idea to develop code to use the data from two lines to perform calculation...

So how to implement what you are saying here? im a little blur about your approach...

See if this helps:

``````Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
Const inFile As String = "C:\data\PUBLT167_89.txt"
Dim line1 As String
Dim line2 As String
Do
If (line1 = String.Empty) Or (line2 = String.Empty) Then
MessageBox.Show("Error. Not expecting empty line")
Exit Sub
ElseIf (line1.Length <> 390) Or (line2.Length <> 390) Then
MessageBox.Show("Invalid line format")
Exit Sub
End If

Dim l1 As LineSetup = LineSetup.ParseLine(line1)
Dim l2 As LineSetup = LineSetup.ParseLine(line2)

If (l1 Is Nothing) Or (l2 Is Nothing) Then
MessageBox.Show("Parsing failed.")
Exit Sub
ElseIf (l1.LineNumber <> l2.LineNumber) Then
MessageBox.Show("Did not find the same line numbers twice in a row")
End If

Console.WriteLine(l1.LineNumber)

'Dim len As Integer = line.Length
'Dim lineNumber1 As String = line.Substring(30, 3) + "-" + line.Substring(84, 3)
'System.Diagnostics.Debugger.Break()
Loop Until line1 Is Nothing Or line2 Is Nothing
End Using
End Sub

Private Class LineSetup
Private _linePart1 As Integer
Private _linePart2 As Integer
Public ReadOnly Property LineNumber() As String
Get
Return String.Format("{0:F0}-{1:F0}", _linePart1, _linePart2)
End Get
End Property
Get
End Get
End Property
Get
End Get
End Property
Get
End Get
End Property
Private Sub New()

End Sub
Public Shared Function ParseLine(ByVal line As String) As LineSetup
If (String.IsNullOrEmpty(line)) Then
Return Nothing
ElseIf (line.Length <> 390) Then
Return Nothing
End If
Dim result As New LineSetup()
result._linePart1 = Convert.ToInt32(line.Substring(30, 3))
result._linePart2 = Convert.ToInt32(line.Substring(84, 3))

Dim chunk As String() = line.Substring(382, 8).Split(".")

Return result
End Function
End Class``````

here are my code with my calculation...

``````Imports System.Math

Public Class Form1
Dim x0 As Double
Dim x As Double
Dim x1 As Integer
Dim x2 As Double
Dim x3 As Double
Dim xx As Integer
Dim xxx As Integer
Dim xxs As Double
Dim deg As Integer
Dim deg1 As Double
Dim deg2 As Double
Dim avg As Double
Dim degl As Double
Dim minn As Integer
Dim secc As Double
Dim seccc As Integer

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Const inFile As String = "C:\sob.txt"
'Using sw As New System.IO.StreamWriter("c:\sob_out.txt", False)
Dim line1 As String
Dim line2 As String
Do
'MsgBox(line1)
'MsgBox(line2)
If (line1 = String.Empty) Or (line2 = String.Empty) Then
MessageBox.Show("Error. Not expecting empty line")
Exit Sub
ElseIf (line1.Length <> 390) Or (line2.Length <> 390) Then
MessageBox.Show("Invalid line format")
Exit Sub
End If

Dim l1 As LineSetup = LineSetup.ParseLine(line1)
Dim l2 As LineSetup = LineSetup.ParseLine(line2)
'MsgBox(l1)
'MsgBox(l2)
If (l1 Is Nothing) Or (l2 Is Nothing) Then
MessageBox.Show("Parsing failed.")
Exit Sub
ElseIf (l1.LineNumber <> l2.LineNumber) Then
MessageBox.Show("Did not find the same line numbers twice in a row")
End If

'MsgBox(iSomeVal)

rad1 = deg1 * PI / 180
rad2 = deg2 * PI / 180

'degl = avg * 180 / PI
'MsgBox(degl)
Call dms(avg, deg, minn, secc)
seccc = Round(secc, 1)
'MsgBox(deg)
'MsgBox(minn)
'MsgBox(seccc)
'Dim len As Integer = line.Length
'Dim lineNumber1 As String = line.Substring(30, 3) + "-" + line.Substring(84, 3)
'System.Diagnostics.Debugger.Break()
Loop Until line1 Is Nothing Or line2 Is Nothing
End Using
End Sub

Public Sub dms(ByVal xxxx As Double, ByRef deg As Integer, ByRef min As Integer, ByRef sec As Double)
x = xxxx * 180 / PI
'MsgBox(x)
Call rounddown(x, xx)
Call degree(x, xx, xxs)
deg = xx
'MsgBox(deg)
Call rounddown(xxs, xxx)
Call minute(xxx, x, deg, min, sec)
'MsgBox(min)
'MsgBox(sec)
End Sub
Public Sub rounddown(ByVal deci As Double, ByRef x4 As Integer)
x0 = deci
'MsgBox(deci)
x1 = x0
'MsgBox(x1)
x2 = x0 - x1
'MsgBox(x2)
If x2 < 0 Then
x3 = x0 - 1
x4 = Round(x3)
'MsgBox(x4)
ElseIf x2 > 0 Then
x4 = Round(x0)
End If
End Sub
Public Sub degree(ByVal x As Double, ByVal xx As Integer, ByRef xxs As Double)
deg = xx
xxs = (x - deg) * 60
'MsgBox(deg)
'MsgBox(xxs)
End Sub
Public Sub minute(ByVal xxx As Integer, ByVal x As Double, ByVal deg As Integer, ByRef min As Integer, ByRef sec As Double)
min = xxx
sec = (((x - deg) * 60) - min) * 60
End Sub

Private Class LineSetup
Private _linePart1 As Integer
Private _linePart2 As Integer
Public ReadOnly Property LineNumber() As String
Get
Return String.Format("{0:F0}-{1:F0}", _linePart1, _linePart2)
End Get
End Property
Get
End Get
End Property
Get
End Get
End Property
Get
End Get
End Property
Private Sub New()

End Sub
Public Shared Function ParseLine(ByVal line As String) As LineSetup
If (String.IsNullOrEmpty(line)) Then
Return Nothing
ElseIf (line.Length <> 390) Then
Return Nothing
End If
Dim result As New LineSetup()
result._linePart1 = Convert.ToInt32(line.Substring(30, 3))
result._linePart2 = Convert.ToInt32(line.Substring(84, 3))

Dim chunk As String() = line.Substring(382, 8).Split(".")

Return result
End Function
End Class
End Class``````

but how can i export the data for Deg, Minn and Seccc to the output file?

i want to export with the format Deg-Minn-Seccc
For the example:
102-12-45
105-43-12

now its all already done... Thanks a lot sknake....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.