From: Keith (Southend)G on 8 Jan 2010 14:37 On Jan 8, 7:04 pm, "Captain Jack" <CaptainJack1...(a)comcast.net> wrote: > "Keith (Southend)G" <keith_harr...(a)hotmail.com> wrote in message > > news:524ab647-438f-422b-822a-08260827d171(a)u7g2000yqm.googlegroups.com... > > > We have output Jack :-) > > Excellent, I love it when a program comes together. :-D > > > This is great Jack, I was gonna say I' feel I've achieved something, > > but I can't take the credit ;-) > > Nah, it's a team effort. > > -- > Jack Now, just been looking through the out put and what I need it to do is check that one line of data doesn't spill into 2 or 3 lines eg... Base Data ################################################################################ # SYNOPS from 72375, Flagstaff (United States) | 35-08-15N | 111-40-12W | 2135 m ################################################################################ 200912301156 AAXX 30124 72375 35966 02704 11061 21100 37862 40195 58001 91156 333 10017 21067 70003 555 93012= Output 30124 72375 35966 02704 11061 21100 37862 40195 58001 91156 I was trying to work out why many of the lines didn't have an '=' at the end of them. This is good, I can start to see how works now, and hopefully build my knowledge on the parsing methods. I just couldn't find this sort of thing when searching on Google. Thanks Keith (Southend)
From: Keith (Southend)G on 10 Jan 2010 10:07 One thing that I'm sure is quite straight forward, when you know how, that I wonder how you would write it? 1. The following works fine but need a little refinement. What code would you need to get it to roll through 2 or 3 lines, including the data, until it gets to the '='. Currently the result in the following example is: 30124 88963 31360 72510 10012 21026 30006 40022 58001 70272 85670 when I would like to have it like this (All on one line, no wrap): 30124 88963 31360 72510 10012 21026 30006 40022 58001 70272 85670 333 10014 21035 41000 56560 82708 85710 86360 90961 92417 93100 95000= eg: Base data... 200912301200 AAXX 30124 88963 31360 72510 10012 21026 30006 40022 58001 70272 85670 333 10014 21035 41000 56560 82708 85710 86360 90961 92417 93100 95000= While input IsNot Nothing ' Assuming input is "200912301350 AAXX [...] 333 88/04=" Dim ItemList() As String = Split(input, " ") If ItemList.Length > 2 AndAlso ItemList(1) = "AAXX" Then Dim Year As Integer = CInt(ItemList (0).Substring(0, 4)) Dim Month As Integer = CInt(ItemList (0).Substring(4, 2)) Dim Day As Integer = CInt(ItemList(2).Substring (0, 2)) Dim Hours As Integer = CInt(ItemList (2).Substring(2, 2)) Dim LineDate As Date = DateSerial(Year, Month, Day).AddHours(Hours) Dim NewLine As String = ItemList(2) For Counter As Integer = 3 To ItemList.Length - 1 NewLine &= " " & ItemList(Counter) Next objWriter.WriteLine(NewLine) End If Thanks Keith (Southend)
From: Captain Jack on 11 Jan 2010 13:15 "Keith (Southend)G" <keith_harris9(a)hotmail.com> wrote in message news:8ac76eba-f106-4962-8aef-dc47cacfb8ae(a)z41g2000yqz.googlegroups.com... > One thing that I'm sure is quite straight forward, when you know how, > that I wonder how you would write it? Making a loop to read for a variable number of lines and then another loop to process it all in the same routine is probably going to end up hard to read and debug. As a good first step, I'd suggest moving the code to read your input lines to a new function, and your code to process the output lines to yet another one. You can replace all of your processing code with this, as an example: ------------------------------------------------------------ Private Const InputFolder As String = "C:\" Private Const InputFile As String = InputFolder & "ogimet.txt" Private Const OutputFolder As String = "C:\" Private Const OutputFile As String = OutputFolder & "sorted.txt" Private Sub ProcessInput() Dim Reader As New System.IO.StreamReader(InputFile, _ System.Text.Encoding.ASCII) Dim Writer As New System.IO.StreamWriter(OutputFile, _ False, System.Text.Encoding.ASCII) Dim InputLine As String = "" Do InputLine = GetNextLine(Reader) If InputLine > "" Then Dim OutputLine As String = ParseInput(InputLine) If OutputLine > "" Then Writer.WriteLine(OutputLine) End If End If Loop Until InputLine = "" Writer.Close() Reader.Close() End Sub Private Function GetNextLine(ByVal FromReader As System.IO.StreamReader) _ As String Dim Result As String = "" Dim InsideDataLine As Boolean = False Dim Buffer As String = "" Do Buffer = FromReader.ReadLine If Buffer IsNot Nothing Then If Not InsideDataLine Then If Buffer.Length >= 4 AndAlso _ Buffer.Substring(0, 4) Like "2[0-9][0-9][0-9]" Then Result = Buffer.Trim If Result.EndsWith("=") Then Exit Do End If InsideDataLine = True End If Else Result &= " " & Buffer.Trim If Buffer.EndsWith("=") Then Exit Do End If End If End If Loop Until Buffer Is Nothing Return Result End Function Private Function ParseInput(ByVal InputString As String) As String Dim ItemList() As String = Split(InputString, " ") Dim NewLine As String = "" If ItemList.Length > 2 AndAlso ItemList(1) = "AAXX" Then Dim Year As Integer = CInt(ItemList(0).Substring(0, 4)) Dim Month As Integer = CInt(ItemList(0).Substring(4, 2)) Dim Day As Integer = CInt(ItemList(2).Substring(0, 2)) Dim Hours As Integer = CInt(ItemList(2).Substring(2, 2)) Dim LineDate As Date = DateSerial(Year, Month, Day).AddHours(Hours) NewLine = ItemList(2) For Counter As Integer = 3 To ItemList.Length - 1 NewLine &= " " & ItemList(Counter) Next End If Return NewLine End Function ------------------------------------------------------------ Note that all of this code goes outside of any other function or sub you have in the form class. In the event handler function for your button, you can just call the ProcessInput sub. The ProcessInput sub opens a reader and a writer, then it calls the GetNextLine function to return the next complete line from the input reader. GetNextLine loops through the input until it finds a line beginning with the digit "2" followed by three other digits. It then checks that line and all subsequent lines until it finds one that ends with an equals sign ("="). When it does, it returns a single line made up of all the lines from the first one found through the last one. If it doesn't find any lines to send, it returns an empty string, so the ProcessInput sub knows when it's done. The ParseInput function takes a line as created by GetNextLine, and turns it into output. Right now, the function is trying to return every line. If you want to add code to throw some lines away, then the function would return an empty string so that ProcessInput knows not to use it. When ProcessInput gets a non-empty string back from ParseInput, it uses the writer to send the data out. Down the road, you may end up with several writers, if you want data for separate dates to go into separate files. For that, it may be best to use a sorted dictionary to sort the data by date, so you can process everything for each date at the same time. For that, I'd modify ParseInput to add the cleaned up data to the dictionary rather than returning a string, and I'd make a new function for dumping the data. -- Jack
From: Keith (Southend)G on 13 Jan 2010 16:38 On Jan 11, 6:15 pm, "Captain Jack" <CaptainJack1...(a)comcast.net> wrote: > "Keith (Southend)G" <keith_harr...(a)hotmail.com> wrote in message Hi Jack, A lot of errors here, looks like I need to be declaring a few things or something to do with Local / Private. Sorry to have to post the whole code, but you may see where it's going wrong. I tried a few things, like removing the 'Private', which cut down the errors, but I'm sure this does something that's required. Error 1 to 4 'Private' is not valid on a local constant declaration. C: \Documents and Settings\Keith\My Documents\Visual Studio 2008\Projects \Open Text\Open Text\Form1.vb 31 9 Open Text Error 5 Statement cannot appear within a method body. End of method assumed. C:\Documents and Settings\Keith\My Documents\Visual Studio 2008\Projects\Open Text\Open Text\Form1.vb 36 5 Open Text Error 6 Name 'InputFile' is not declared. C:\Documents and Settings \Keith\My Documents\Visual Studio 2008\Projects\Open Text\Open Text \Form1.vb 37 50 Open Text Error 7 Expression expected. C:\Documents and Settings\Keith\My Documents\Visual Studio 2008\Projects\Open Text\Open Text\Form1.vb 40 51 Open Text Error 8 Property access must assign to the property or use its value. C:\Documents and Settings\Keith\My Documents\Visual Studio 2008\Projects\Open Text\Open Text\Form1.vb 41 1 Open Text Error 9 End of statement expected. C:\Documents and Settings\Keith\My Documents\Visual Studio 2008\Projects\Open Text\Open Text\Form1.vb 41 27 Open Text Error 10 Method arguments must be enclosed in parentheses. C: \Documents and Settings\Keith\My Documents\Visual Studio 2008\Projects \Open Text\Open Text\Form1.vb 41 27 Open Text Error 11 & 12 Name 'Writer' is not declared. C:\Documents and Settings \Keith\My Documents\Visual Studio 2008\Projects\Open Text\Open Text \Form1.vb 48 21 Open Text Error 13 Declaration expected. C:\Documents and Settings\Keith\My Documents\Visual Studio 2008\Projects\Open Text\Open Text\Form1.vb 102 21 Open Text Error 14 & 15 'End While' must be preceded by a matching 'While'. C: \Documents and Settings\Keith\My Documents\Visual Studio 2008\Projects \Open Text\Open Text\Form1.vb 103 17 Open Text Error 16,17,19 Declaration expected. C:\Documents and Settings\Keith \My Documents\Visual Studio 2008\Projects\Open Text\Open Text\Form1.vb 105 13 Open Text Error 18 Statement cannot appear outside of a method body. C: \Documents and Settings\Keith\My Documents\Visual Studio 2008\Projects \Open Text\Open Text\Form1.vb 107 9 Open Text Error 20 'End If' must be preceded by a matching 'If'. C:\Documents and Settings\Keith\My Documents\Visual Studio 2008\Projects\Open Text \Open Text\Form1.vb 109 9 Open Text Error 21 'End Sub' must be preceded by a matching 'Sub'. C:\Documents and Settings\Keith\My Documents\Visual Studio 2008\Projects\Open Text \Open Text\Form1.vb 110 5 Open Text Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim FILE_NAME As String = "C:\ogimet.txt" If System.IO.File.Exists(FILE_NAME) = True Then Dim objReader As New System.IO.StreamReader(FILE_NAME) RichTextBox1.Text = objReader.ReadToEnd objReader.Close() Else MsgBox("File Does Not Exist") End If End Sub Private Sub TabPage1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) End Sub Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged End Sub Private Sub RichTextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox2.TextChanged End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Private Const InputFolder As String = "C:\" Private Const InputFile As String = InputFolder & "ogimet.txt" Private Const OutputFolder As String = "C:\" Private Const OutputFile As String = OutputFolder & "sorted.txt" Private Sub ProcessInput() Dim Reader As New System.IO.StreamReader(InputFile, _ System.Text.Encoding.ASCII) Dim Writer As New System.IO.StreamWriter(OutputFile, _ False, System.Text.Encoding.ASCII) Dim InputLine As String = "" Do InputLine = GetNextLine(Reader) If InputLine > "" Then Dim OutputLine As String = ParseInput(InputLine) If OutputLine > "" Then Writer.WriteLine(OutputLine) End If End If Loop Until InputLine = "" Writer.Close() Reader.Close() End Sub Private Function GetNextLine(ByVal FromReader As System.IO.StreamReader) _ As String Dim Result As String = "" Dim InsideDataLine As Boolean = False Dim Buffer As String = "" Do Buffer = FromReader.ReadLine If Buffer IsNot Nothing Then If Not InsideDataLine Then If Buffer.Length >= 4 AndAlso _ Buffer.Substring(0, 4) Like "2[0-9][0-9][0-9]" Then Result = Buffer.Trim If Result.EndsWith("=") Then Exit Do End If InsideDataLine = True End If Else Result &= " " & Buffer.Trim If Buffer.EndsWith("=") Then Exit Do End If End If End If Loop Until Buffer Is Nothing Return Result End Function Private Function ParseInput(ByVal InputString As String) As String Dim ItemList() As String = Split(InputString, " ") Dim NewLine As String = "" If ItemList.Length > 2 AndAlso ItemList(1) = "AAXX" Then Dim Year As Integer = CInt(ItemList(0).Substring(0, 4)) Dim Month As Integer = CInt(ItemList(0).Substring(4, 2)) Dim Day As Integer = CInt(ItemList(2).Substring(0, 2)) Dim Hours As Integer = CInt(ItemList(2).Substring(2, 2)) Dim LineDate As Date = DateSerial(Year, Month, Day).AddHours(Hours) NewLine = ItemList(2) For Counter As Integer = 3 To ItemList.Length - 1 NewLine &= " " & ItemList(Counter) Next End If Return NewLine End Function input = sr.ReadLine() End While End Using objWriter.Close() MsgBox("Text written to file") Else MsgBox("File Does Not Exist") End If End Sub End Class This has been very useful as it has made me realize how much there is to this and that I need to understand precisely what each line is doing, which you explained well in your previous post. It's a shame I couldn't unroll on a class locally, but you only get very basic computer courses, not programming and the like. It's frustrating as I could save myself so much time and anguish, the trouble is the weather data never stops, so I'm chasing rainbows, which uses the time I need to learn this. However, I'm not giving up yet ;-) Many thanks Keith (Southend)
From: Captain Jack on 13 Jan 2010 16:51
"Keith (Southend)G" <keith_harris9(a)hotmail.com> wrote in message news:2d5e108a-193f-46e9-b0d5-d163f51ce54f(a)22g2000yqr.googlegroups.com... > > This has been very useful as it has made me realize how much there is > to this and that I need to understand precisely what each line is > doing, which you explained well in your previous post. It's a shame I > couldn't unroll on a class locally, but you only get very basic > computer courses, not programming and the like. It's frustrating as I > could save myself so much time and anguish, the trouble is the weather > data never stops, so I'm chasing rainbows, which uses the time I need > to learn this. However, I'm not giving up yet ;-) > > Many thanks > > Keith (Southend) Sure thing. :-) Looks like it should be easy to fix. Everything that's inside the sub Button2_Click needs to go outside of it. Some of the declarations (like Private Const) can't be inside the sub definition, they have to be in the class definition. If you move all of that code out (say, put it after the End Sub) then Button2_Click just needs to look like this: Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click ProcessInput() End Sub What will happen then is that, when Button2 is clicked, th ProcessInput sub will be run, which will in turn run the other subs. But all of the subs, functions, and variables declared with Private scope have to be outside of the sub; that way, VB knows where to find them. Right now, VB is seeing those things inside of the sub, and it just doesn't like them to be there. -- Jack |