|
-
Jan 14th, 2007, 10:33 PM
#1
Thread Starter
Lively Member
[RESOLVED] Parsing text file
Last edited by tiguy; Feb 12th, 2009 at 09:51 PM.
-
Jan 14th, 2007, 10:45 PM
#2
Re: Parsing text file
Can you post a snippet of the file, and the value of 'ln'?
-
Jan 14th, 2007, 10:48 PM
#3
Thread Starter
Lively Member
Last edited by tiguy; Feb 12th, 2009 at 09:49 PM.
-
Jan 14th, 2007, 10:53 PM
#4
Re: Parsing text file
Give me a few
-
Jan 14th, 2007, 10:55 PM
#5
Thread Starter
Lively Member
Re: Parsing text file
Hey is there any way better that would look at the "F" And the number to the right of it and if that number was greater than 1.3 it would change it to 1.3?
Sorry for all the new ideas.
Thanks
Reston
-
Jan 14th, 2007, 11:08 PM
#6
Re: Parsing text file
VB Code:
Option Explicit
Private Sub Command1_Click()
Dim intFF As Integer
Dim lngCount As Long, lngIdx As Long
Dim strArr() As String
On Error GoTo Err_Handler
intFF = FreeFile
'Load the TextFile into an Array
Open App.Path & "\test.txt" For Input As #intFF
strArr() = Split(Input(LOF(intFF), 1), vbCrLf)
Close #intFF
'Now, pass tru each Array element and look for a match
lngCount = UBound(strArr) - 1
For lngIdx = 0 To lngCount
If InStr(1, strArr(lngIdx), "F", vbTextCompare) <> 0 And Left$(strArr(lngIdx), 1) <> "(" _
Then MsgBox Trim$(Right$(strArr(lngIdx), Len(strArr(lngIdx)) - InStrRev(strArr(lngIdx), "F", -1, vbTextCompare)))
Next
Exit Sub
Err_Handler:
MsgBox "Number: " & Err.Number & vbCrLf & _
"Description: " & Err.Description, vbOKOnly + vbInformation, "Error!"
End Sub
Tested with the above snippet.
Last edited by Bruce Fox; Jan 14th, 2007 at 11:12 PM.
-
Jan 14th, 2007, 11:20 PM
#7
Thread Starter
Lively Member
Re: Parsing text file
Hey Bruce Fox did you see my last post?
-
Jan 14th, 2007, 11:26 PM
#8
Re: Parsing text file
Yes I did!
Did you try it, if it is on track THEN we can tweek it
-
Jan 14th, 2007, 11:55 PM
#9
Re: Parsing text file
Will the header be always the same or there's a pattern to it? Same number of lines, etc? Will there be other instances of F such as in trailer info?
Cause if we can truncate the leading and trailing chars, leaving us with N* then we can Split on F.
-
Jan 15th, 2007, 05:56 PM
#10
Thread Starter
Lively Member
Re: Parsing text file
Hi Bruce
That worked fine for the msgbox. And can we add the line number that is in the beginning of the "F" line. That is the N20G1Z0.F1.3 so the opperator knows where the line is that the "F" is on?
Just so everyone know these are cnc gcode files that i'm working on.The length of the files are all over the place. can be up to 100000 lines.
Thanks again for the help
Reston
-
Jan 15th, 2007, 06:00 PM
#11
Re: Parsing text file
So the format for the Line Num is for example:N20G1Z0?
Also, what do you want to do with the numbers after "F". Display as they are below the value of 1.3, and if above 1.3 only display as 1.3?
-
Jan 15th, 2007, 10:05 PM
#12
Thread Starter
Lively Member
Re: Parsing text file
Bruce
The N20 is the line number. I think it would be nice to just display the whole line that has an "F" in it. Example: N20G1Z0.F1.3 That way it would be easier to find later if we find a line that the feed rate is set to high.
-
Jan 15th, 2007, 10:13 PM
#13
Re: Parsing text file
I had done this earlier for you pending a response. Adjust as required.
VB Code:
Option Explicit
Private Sub Command1_Click()
Dim intFF As Integer
Dim lngCount As Long, lngIdx As Long
Dim strArr() As String
On Error GoTo Err_Handler
intFF = FreeFile
'Load the TextFile into an Array
Open App.Path & "\test.txt" For Input As #intFF
strArr() = Split(Input(LOF(intFF), 1), vbCrLf)
Close #intFF
'Now, pass tru each Array element and look for a match
lngCount = UBound(strArr) - 1
For lngIdx = 0 To lngCount
If InStr(1, strArr(lngIdx), "F", vbTextCompare) <> 0 And Left$(strArr(lngIdx), 1) <> "(" Then
If IsNumeric(Trim$(Right$(strArr(lngIdx), Len(strArr(lngIdx)) - InStrRev(strArr(lngIdx), "F", -1, vbTextCompare)))) Then
'Line Number data
MsgBox Trim$(Left$(strArr(lngIdx), InStrRev(strArr(lngIdx), "F", -1, vbTextCompare) - 1))
'Value
MsgBox Trim$(Right$(strArr(lngIdx), Len(strArr(lngIdx)) - InStrRev(strArr(lngIdx), "F", -1, vbTextCompare)))
End If
End If
Next
Exit Sub
Err_Handler:
MsgBox "Number: " & Err.Number & vbCrLf & _
"Description: " & Err.Description, vbOKOnly + vbInformation, "Error!"
End Sub
Yeilds, Line Number, Value, Line Number, Value:
N20G1Z0.
1.3
N46X-.1567Y2.2957
200.250
-
Jan 15th, 2007, 10:14 PM
#14
Re: Parsing text file
Do you still want only a COMPLETE line that has the F and followed by Numerics?
-
Jan 16th, 2007, 12:19 AM
#15
Hyperactive Member
Re: Parsing text file
Just tested this, and it works. To add what line it's on as you've stated earlier, just add an extra variable that counts the number of loops gone by, by adding +1 at the end of each loop(i.e. each time a line is loaded):
VB Code:
Dim ln As String, keyword As String: keyword = "F"
Dim ff As Integer, lnNum As String: ff = FreeFile
'Open App.Path & "\nifchwc.t" For Input As #ff
Open "C:\Nif2\nif2chwc\nifchwc.t" For Input As #ff
Do Until EOF(ff)
Line Input #ff, ln
If InStr(1, ln, keyword) Then
lnNum = Mid(ln, InStr(ln, keyword) + 1)
If Val(lnNum) > 1.3 Then ln = Replace$(ln, lnNum, "1.3")
Debug.Print ln
'Printer.Print ln
End If
ace = 77 'Just a note, this is not declared in your code!
Loop
Close #ff
This will, however, replace both numbers after "F" to "1.3" like you said. To adjust it to make the second number NOT change to "1.3" and change to something else, just add an extra check to see if it is higher than whatever number you expect to be there, ya dig?
God put me on this earth to do many great things, and I'm so far behind that I'm going to live forever.
I'm programming for Windows using a Apple Mac Mini, 1.5Ghz with 512MB DDR RAM. I feel like I'm committing a crime :P
-
Jan 16th, 2007, 08:35 PM
#16
Thread Starter
Lively Member
Re: Parsing text file
Sorry
I've had alot going on and have not had a chance to try some of these.
Give me a few
-
Jan 17th, 2007, 09:12 PM
#17
Thread Starter
Lively Member
Re: [RESOLVED] Parsing text file
Hey Thanks everyone for helping. Lots to learn!
This is what I'm going with
VB Code:
Private Sub Command4_Click()
'Option Explicit
Dim intFF As Integer
Dim lngCount As Long, lngIdx As Long
Dim strArr() As String
On Error GoTo Err_Handler
intFF = FreeFile
'Load the TextFile into an Array
Open App.Path & "\" & Text1.Text For Input As #intFF
strArr() = Split(Input(LOF(intFF), 1), vbCrLf)
Close #intFF
'Now, pass tru each Array element and look for a match
lngCount = UBound(strArr) - 1
For lngIdx = 0 To lngCount
If InStr(1, strArr(lngIdx), "F", vbTextCompare) <> 0 And Left$(strArr(lngIdx), 1) <> "(" Then
If IsNumeric(Trim$(Right$(strArr(lngIdx), Len(strArr(lngIdx)) - InStrRev(strArr(lngIdx), "F", -1, vbTextCompare)))) Then
MsgBox Trim$(Right$(strArr(lngIdx), Len(strArr(lngIdx)) - InStrRev(strArr(lngIdx), "F", 1, vbTextCompare)))
End If
End If
Next
Exit Sub
Err_Handler:
MsgBox "Number: " & Err.Number & vbCrLf & _
"Description: " & Err.Description, vbOKOnly + vbInformation, "Error!"
End Sub
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
|