-
Oct 13th, 2017, 01:46 PM
#41
Re: Is this program possible to make in VB .NET?
When you're ready to rip your hair out, start reading about regular expressions. Si_The_Geeks advice triggered a nerve. It's a good thing to learn.
-
Oct 14th, 2017, 06:00 AM
#42
Thread Starter
Member
Re: Is this program possible to make in VB .NET?
One thing that is confusing me, I may be getting tripped up on something silly here:
If TextLine <> "" And Not TextLine Like "*[A-Za-z]*" Then
End If
Obviously some code to skip to the next line would go in this if statement, but I can't figure out how to skip it.
Edit: would the line to skip to the next line of text be: objReader.ReadLine()
I just confirmed that this is correct.
Last edited by Astralogic; Oct 14th, 2017 at 06:19 AM.
-
Oct 14th, 2017, 06:21 AM
#43
Re: Is this program possible to make in VB .NET?
I was under the impression you wanted to run some code when that If statement is true (so the line is not blank, and there are also no letters).
If that is correct, the code should be something like this:
Code:
Dim TextLine As String = objReader.ReadLine()
Do While objReader.Peek() <> -1
If TextLine <> "" And Not TextLine Like "*[A-Za-z]*" Then
'code here to work with TextLine
End If
TextLine = objReader.ReadLine()
Loop
-
Oct 14th, 2017, 06:36 AM
#44
Thread Starter
Member
Re: Is this program possible to make in VB .NET?
I modified the outline to reflkect the changes.
Code:
Do:
Read a line.
If it's blank or contains text:
Forget this line. It's garbage.
Else
This is a data line.
Split the data line into individual tokens.
Store the tokens
Until we have read the entire file.
I'm on the storing part. Would this be stored in an array and if so, what size do I initialze the array as?
-
Oct 14th, 2017, 07:47 AM
#45
Re: Is this program possible to make in VB .NET?
The If statement does the equivalent of all of this:
Code:
If it's blank or contains text:
Forget this line. It's garbage.
Else
...so where I put "code here to work with TextLine", you would do these things:
Code:
Split the data line into individual tokens.
Store the tokens
Originally Posted by Astralogic
I'm on the storing part. Would this be stored in an array and if so, what size do I initialze the array as?
There are a variety of options there, and it could be a fairly big discussion by itself... given that this thread is so long, and so far away from the original question, it would be best to start a new thread at this point (I just spent a few minutes trying to work out the situation for this part, but it's hard to find in the mass of posts that aren't relevant).
One thing to think about is whether or not you know the largest number that could be in the file... if you do, it could make things much simpler.
-
Oct 14th, 2017, 07:37 PM
#46
Re: Is this program possible to make in VB .NET?
I feel like we're in the weeds. I think you need to clarify what you meant in #34 when you said "I want to ignore lines with text". I think you mean you just want the numbers. It turns out this code's much harder to implement if you see that as meaning, "I need to inspect the line and see if it's a list of numbers. You don't, if you get a little clever.
"Store something" in programming always means "you need a variable". In this case, we want a LOT of things. There's a line that represents a name, and then there are a lot of numbers. When we want to "store many different things", it means "we need a class".
So each pair of lines in the file represents a class like this:
Code:
Public Class Numbers
Public Property Name As String
Public Property Items As Integer()
End Class
Lots of people say classes are "too advanced" for them. This problem's much more complex without them.
It sounds like there might be many spaces or other inconsistencies in the data. Whatever. None of these problems are not hard to solve and don't require the kinds of power tools being waved around. Worst-case scenario, we write our own String.Split(), and that takes 5 whole minutes once you aren't afraid of it.
We need to open the file with a StreamReader, because ReadLine() is going to be really useful here. We're going to be looking at a space-delimited String, so we'll be calling String.Split(). We're going to be parsing Integers, and we're worried they might not be real numbers, so we'll want to use Integer.TryParse(). Here's how I'd implement my algorithm:
Code:
' ((There might be some tweaks needed, I'm not writing this in
' Visual Studio. It ought to be close enough the documentation
' and some intuition can guide you.))
Dim results As New List(Of Numbers)()
Dim currentResult As Numbers = Nothing
Using reader as New StreamReader(yourPath)
While Not reader.EndOfStream
Dim currentLine = reader.ReadLine()
If String.IsNullOrWhitespace(currentLine) Then
Continue While
Else If currentResult Is Nothing Then
currentResult = New Numbers()
currentResult.Name = currentLine
Else
Dim separators() As Char = {" ", vbTab}
Dim tokens = currentLine.Split(separators, StringSplitOptions.RemoveEmptyEntries)
Dim parsed As New List(Of Integer)()
Dim output As Integer
For Each token In tokens
If Integer.TryParse(token, output) Then
parsed.Add(output)
Else
' Bad data: log here.
End If
Next
currentResult.Items = parsed.ToArray()
results.Add(currentResult)
currentResult = Nothing
End If
End While
End Using
The first few lines set up your basic "read the entire file" ReadLine() loop.
If we find a blank line, it's completely ignored. This is checked first so we don't try to do anything with it.
If the line's not blank, we check to see if we have a "current result". If we don't, we assume this line must be a "name" since those come first. The "current result" is set, and the current line is stored as its name.
If the line's not blank and we have a "current result", we assume we just read the "name" line and this line must be numbers. String.Split() will give us just the numbers, and the RemoveEmptyEntries option does what it says and deals with multiple spaces. Next, we use TryParse() instead of just Parse() to convert the Strings to numbers, this gives us a chance to log/inspect any values that aren't numbers. Then, the parsed list of numbers is saved in the current result, which is then added to the list of results. Finally, the "current result" is reset to Nothing so the next time we find a nonblank line we interpret it as a "name".
So the problem isn't "ignore any non-number, non-blank line". Just "When we figure out the line's blank, make sure we've already read a line before assuming we have numbers." If we get it wrong, you ought to spit out an Integer.TryParse() failure if you add logging code, then you can figure out what's different in that part of the file.
That's about it. No "stupid pet tricks" version this time, it requires too many silly things like "using the Reactive Extensions" or "writing a new LINQ operator". (Which really means I'm doing it for fun but not waiting until I'm done to post this.)
Last edited by Sitten Spynne; Oct 14th, 2017 at 08:58 PM.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
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
|