Results 1 to 17 of 17

Thread: [RESOLVED] Parsing text file

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2001
    Location
    Ca
    Posts
    124

    Resolved [RESOLVED] Parsing text file

    Hi all
    Last edited by tiguy; Feb 12th, 2009 at 09:51 PM.

  2. #2
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Parsing text file

    Can you post a snippet of the file, and the value of 'ln'?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2001
    Location
    Ca
    Posts
    124

    Re: Parsing text file

    text file
    Last edited by tiguy; Feb 12th, 2009 at 09:49 PM.

  4. #4
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Parsing text file

    Give me a few

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2001
    Location
    Ca
    Posts
    124

    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

  6. #6
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Parsing text file

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4. Dim intFF As Integer
    5. Dim lngCount As Long, lngIdx As Long
    6. Dim strArr() As String
    7.  
    8. On Error GoTo Err_Handler
    9.  
    10.     intFF = FreeFile
    11.  
    12.     'Load the TextFile into an Array
    13.     Open App.Path & "\test.txt" For Input As #intFF
    14.         strArr() = Split(Input(LOF(intFF), 1), vbCrLf)
    15.     Close #intFF
    16.  
    17.     'Now, pass tru each Array element and look for a match
    18.     lngCount = UBound(strArr) - 1
    19.    
    20.     For lngIdx = 0 To lngCount
    21.         If InStr(1, strArr(lngIdx), "F", vbTextCompare) <> 0 And Left$(strArr(lngIdx), 1) <> "(" _
    22.             Then MsgBox Trim$(Right$(strArr(lngIdx), Len(strArr(lngIdx)) - InStrRev(strArr(lngIdx), "F", -1, vbTextCompare)))
    23.     Next
    24.  
    25. Exit Sub
    26.  
    27. Err_Handler:
    28.     MsgBox "Number: " & Err.Number & vbCrLf & _
    29.     "Description: " & Err.Description, vbOKOnly + vbInformation, "Error!"
    30.  
    31. End Sub

    Tested with the above snippet.
    Last edited by Bruce Fox; Jan 14th, 2007 at 11:12 PM.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2001
    Location
    Ca
    Posts
    124

    Re: Parsing text file

    Hey Bruce Fox did you see my last post?

  8. #8
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Parsing text file

    Yes I did!

    Did you try it, if it is on track THEN we can tweek it

  9. #9
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    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.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jan 2001
    Location
    Ca
    Posts
    124

    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

  11. #11
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    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?

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Jan 2001
    Location
    Ca
    Posts
    124

    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.

  13. #13
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Parsing text file

    I had done this earlier for you pending a response. Adjust as required.
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4. Dim intFF As Integer
    5. Dim lngCount As Long, lngIdx As Long
    6. Dim strArr() As String
    7.  
    8. On Error GoTo Err_Handler
    9.  
    10.     intFF = FreeFile
    11.  
    12.     'Load the TextFile into an Array
    13.     Open App.Path & "\test.txt" For Input As #intFF
    14.         strArr() = Split(Input(LOF(intFF), 1), vbCrLf)
    15.     Close #intFF
    16.  
    17.     'Now, pass tru each Array element and look for a match
    18.     lngCount = UBound(strArr) - 1
    19.  
    20.     For lngIdx = 0 To lngCount
    21.         If InStr(1, strArr(lngIdx), "F", vbTextCompare) <> 0 And Left$(strArr(lngIdx), 1) <> "(" Then
    22.             If IsNumeric(Trim$(Right$(strArr(lngIdx), Len(strArr(lngIdx)) - InStrRev(strArr(lngIdx), "F", -1, vbTextCompare)))) Then
    23.  
    24.                 'Line Number data
    25.                 MsgBox Trim$(Left$(strArr(lngIdx), InStrRev(strArr(lngIdx), "F", -1, vbTextCompare) - 1))
    26.  
    27.                 'Value
    28.                 MsgBox Trim$(Right$(strArr(lngIdx), Len(strArr(lngIdx)) - InStrRev(strArr(lngIdx), "F", -1, vbTextCompare)))
    29.  
    30.             End If
    31.         End If
    32.     Next
    33.  
    34. Exit Sub
    35.  
    36. Err_Handler:
    37.     MsgBox "Number: " & Err.Number & vbCrLf & _
    38.     "Description: " & Err.Description, vbOKOnly + vbInformation, "Error!"
    39.  
    40. End Sub

    Yeilds, Line Number, Value, Line Number, Value:
    N20G1Z0.
    1.3
    N46X-.1567Y2.2957
    200.250

  14. #14
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Parsing text file

    Do you still want only a COMPLETE line that has the F and followed by Numerics?

  15. #15
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    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:
    1. Dim ln As String, keyword As String: keyword = "F"
    2. Dim ff As Integer, lnNum As String: ff = FreeFile
    3.  
    4.    'Open App.Path & "\nifchwc.t" For Input As #ff
    5.     Open "C:\Nif2\nif2chwc\nifchwc.t" For Input As #ff
    6.        Do Until EOF(ff)
    7.             Line Input #ff, ln
    8.                 If InStr(1, ln, keyword) Then
    9.                     lnNum = Mid(ln, InStr(ln, keyword) + 1)
    10.                         If Val(lnNum) > 1.3 Then ln = Replace$(ln, lnNum, "1.3")
    11.                     Debug.Print ln
    12.                     'Printer.Print ln
    13.                 End If
    14.                 ace = 77 'Just a note, this is not declared in your code!
    15.         Loop
    16.     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

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Jan 2001
    Location
    Ca
    Posts
    124

    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

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Jan 2001
    Location
    Ca
    Posts
    124

    Re: [RESOLVED] Parsing text file

    Hey Thanks everyone for helping. Lots to learn!

    This is what I'm going with

    VB Code:
    1. Private Sub Command4_Click()
    2. 'Option Explicit
    3.  
    4. Dim intFF As Integer
    5. Dim lngCount As Long, lngIdx As Long
    6. Dim strArr() As String
    7.  
    8. On Error GoTo Err_Handler
    9.  
    10.     intFF = FreeFile
    11.  
    12.     'Load the TextFile into an Array
    13.     Open App.Path & "\" & Text1.Text For Input As #intFF
    14.     strArr() = Split(Input(LOF(intFF), 1), vbCrLf)
    15.     Close #intFF
    16.  
    17.     'Now, pass tru each Array element and look for a match
    18.     lngCount = UBound(strArr) - 1
    19.  
    20.     For lngIdx = 0 To lngCount
    21.         If InStr(1, strArr(lngIdx), "F", vbTextCompare) <> 0 And Left$(strArr(lngIdx), 1) <> "(" Then
    22.             If IsNumeric(Trim$(Right$(strArr(lngIdx), Len(strArr(lngIdx)) - InStrRev(strArr(lngIdx), "F", -1, vbTextCompare)))) Then
    23.  
    24.                 MsgBox Trim$(Right$(strArr(lngIdx), Len(strArr(lngIdx)) - InStrRev(strArr(lngIdx), "F", 1, vbTextCompare)))
    25.  
    26.                 End If
    27.         End If
    28.     Next
    29.  
    30. Exit Sub
    31.  
    32. Err_Handler:
    33.     MsgBox "Number: " & Err.Number & vbCrLf & _
    34.     "Description: " & Err.Description, vbOKOnly + vbInformation, "Error!"
    35.  
    36. 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
  •  



Click Here to Expand Forum to Full Width