I have an input file that looks like this: ID= 57010101-1 X= 0.0005 Y= 0.0006 Z= 0.0000 Avg= -0.0058 ID= 57010101-2 X= 0.0005 Y= 0.0005 Z= -0.0001 Avg= -0.0057 ID= 57010101-3 X= 0.0005 Y= 0.0007 Z= -0.0001 Avg= -0.0056 C= -0.0031I need to extract the xyz from the id.
I have been messing with this for a while now but i cannot get it to grab it from the specific id that i give it.
Any suggestions?
I am using vb.net
You know, I've never really learned any good methods to parse text files, but even I know it's best to parse the entire file once and be done with it. Consider the following command line program:
Option Infer On Module Module1 Sub Main() Dim vals = parseFile("c:\some.txt") With vals("57010101-2") Console.WriteLine("X={0} Y={1} Z={2}", .X, .Y, .Z) End With Console.ReadKey(True) End Sub Structure valueSet Public X As Double? Public Y As Double? Public Z As Double? Public ReadOnly Property IsValid As Boolean Get Return X.HasValue AndAlso Y.HasValue AndAlso Z.HasValue End Get End Property End Structure Function parseFile(filePath As String) As Dictionary(Of String, valueSet) Dim ret = New Dictionary(Of String, valueSet) Dim file = New System.IO.StreamReader(filePath) Dim id As String Dim values = New valueSet Do While String.IsNullOrEmpty(id) AndAlso Not file.EndOfStream Dim line = Split(file.ReadLine) If line.Length = 2 AndAlso String.Equals("ID=", line(0), _ StringComparison.CurrentCultureIgnoreCase) Then _ id = line(1) Loop Do Until file.EndOfStream Dim line = Split(file.ReadLine) If line.Length = 2 Then Select Case line(0).ToUpperInvariant() Case "ID=".ToUpperInvariant() If values.IsValid Then _ ret.Add(id, values) id = line(1) : values = New valueSet Case "X=".ToUpperInvariant() values.X = Double.Parse(line(1)) Case "Y=".ToUpperInvariant() values.Y = Double.Parse(line(1)) Case "Z=".ToUpperInvariant() values.Z = Double.Parse(line(1)) End Select End If Loop If values.IsValid AndAlso Not ret.ContainsKey(id) Then _ ret.Add(id, values) Return ret End Function End Module
Just read the file line by line until you find the ID that you are looking for. Then read the next 3 lines and extract the X, Y, Z values.
I'm not quite sure how to do that. That's the part I am stuck on
Which part don't you know how to do? Read lines from a text file into a string? Search the string for certain characters or strings? Extract part of a string? Google will help you with all of those.
how to read the next 3 lines after
Function GetValue(ByVal val As String) As String ListBox1.Items.Clear() val &= " " For Each line In IO.File.ReadAllLines("C:\Users\User\Desktop\result.txt") If line.Contains(TextBox1.Text) Then Msgbox(line) End If Next End FunctionWith that I can find the line. But Im not sure how to get the xyz from the next few lines
This is what I came up with: Sub linestr() Dim c As Integer = 1 Dim sr As New System.IO.StreamReader(input) Dim line As String = String.Empty Dim lines As String() ListBox1.Items.Clear() line = sr.ReadLine() Do While (Not line Is Nothing) If line.Contains(TextBox1.Text) Then Label1.Text = c.ToString() lines = IO.File.ReadAllLines(input) Label2.Text = lines(c) ListBox1.Items.Add(lines(c + 2)) 'X ListBox1.Items.Add(lines(c + 3)) 'Y ListBox1.Items.Add(lines(c + 4)) 'Z End If line = sr.ReadLine() Loop End SubAny suggestions on a better way?
is that powershell? (I am totally clueless on powershell,as you can tell.)
Since I don't know that script, I'll try to wing it:
Function GetValue(ByVal val As String) As String
ListBox1.Items.Clear()
val &= " "
x=0
For Each line In IO.File.ReadAllLines("C:\Users\User\Desktop\result.txt")
If line.Contains(TextBox1.Text) Then
x=1
Msgbox(line)
End If
if x>0 then
if x>1 and x<5 then msgbox(line)
x=x+1
end if
Next
End FunctionI know, that is atrocious coding, but like I said, I don't know anything about this language, or even if this will work or not. I guess I need to download or otherwise obtain powershell documentation.
No, its VB.NET but I think your code is cleaner than mine. I feel like I over complicated it.
Oh. I'm so far out of the loop, I think I just passed Pluto! ;-)
I didn't know if you could use subscripts on the lines. In that case, this might be an improvement (assuming it works):
lines=io.file.readalllines(input)
'this next might need to be: for i=0 to lines.count
for i=0 to ubound(lines)
'and this might need to be: if instr(lines(i),textbox1.text) then...
if lines(i).contains(textbox1.text) then
for L=i+1 to i+3
msgbox(lines(L))
next
exit for
end if
next
You know, I've never really learned any good methods to parse text files, but even I know it's best to parse the entire file once and be done with it. Consider the following command line program:
Option Infer On Module Module1 Sub Main() Dim vals = parseFile("c:\some.txt") With vals("57010101-2") Console.WriteLine("X={0} Y={1} Z={2}", .X, .Y, .Z) End With Console.ReadKey(True) End Sub Structure valueSet Public X As Double? Public Y As Double? Public Z As Double? Public ReadOnly Property IsValid As Boolean Get Return X.HasValue AndAlso Y.HasValue AndAlso Z.HasValue End Get End Property End Structure Function parseFile(filePath As String) As Dictionary(Of String, valueSet) Dim ret = New Dictionary(Of String, valueSet) Dim file = New System.IO.StreamReader(filePath) Dim id As String Dim values = New valueSet Do While String.IsNullOrEmpty(id) AndAlso Not file.EndOfStream Dim line = Split(file.ReadLine) If line.Length = 2 AndAlso String.Equals("ID=", line(0), _ StringComparison.CurrentCultureIgnoreCase) Then _ id = line(1) Loop Do Until file.EndOfStream Dim line = Split(file.ReadLine) If line.Length = 2 Then Select Case line(0).ToUpperInvariant() Case "ID=".ToUpperInvariant() If values.IsValid Then _ ret.Add(id, values) id = line(1) : values = New valueSet Case "X=".ToUpperInvariant() values.X = Double.Parse(line(1)) Case "Y=".ToUpperInvariant() values.Y = Double.Parse(line(1)) Case "Z=".ToUpperInvariant() values.Z = Double.Parse(line(1)) End Select End If Loop If values.IsValid AndAlso Not ret.ContainsKey(id) Then _ ret.Add(id, values) Return ret End Function End Module
I like that. I will give it a try. Thank you
Sure. Just save the resulting Dictionary in the module level, so you're not parsing the same file every time the user hits the button.
Yes (14) | ![]() | |
No (14) | ![]() | |
I don't know (15) | ![]() |