Results 1 to 8 of 8

Thread: [RESOLVED] Help for deleting "wrong" lines from a file

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2010
    Posts
    38

    Resolved [RESOLVED] Help for deleting "wrong" lines from a file

    Hello all, i want to examine a file which have specific format for erroneous entries. For example i give u a few lines containing the errors i have found

    Code:
    19997 29 36  3   162  -210  -499 -1018  3420  1770  430875  4  4  2  1  5  3  3201
    20097 29 48  2   182  -212  -510 -1003  3390  1748  246757 32  4 13 18 26 21254621
    00792 11 9   5   -53  -356  -560 -1096  3454  1786  924438  2  2  2  3 10  6 10362
    09407 13 26 12   485    -3  -395  -842  3178  1625  316700%1663%1177639711613352 91616
    20197 30 45  2   163  -211  -498 -1018  3419  1768  431720  2  3  3  4  9  5  5727
    20297 30 33  1   146  -218  -504 -1016  3398  1762  417309  0  0  0  0  0  0     0
    The last value of the 2nd line should have length not more than 6 so as to be printed separated from its previous and the fourth line contains the % character.

    To resolve this problem i use the following code

    Code:
    While Not EOF(1)
    Input #1, a$
    b$ = Right(a$, 6)
    If InStr(a$, "%") = 0 And InStr(b$, "") = 0 Then
    Print #2, a$
    Else
    deleted = deleted + 1
    Print #3, a$
    End If
    Wend
    while it works for the % character i can't find a specific condition for the last value problem. Any help is much appreciated.
    Thank you in advance.

  2. #2
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Help for deleting "wrong" lines from a file

    The % is apparently printed to denote an overflow of one character. This then further lenghthens the column by one character.

    This is how I understand it:
    09407 13 26 12 485 -3 -395 -842 3178 1625 316700%1663%1177639711613352 91616
    Correctly parsed:
    • 09407
    • 13
    • 26
    • 485
    • -3
    • -395
    • -842
    • 3178
    • 1625
    • 316700
    • 1663
    • 1177
    • 639
    • 711
    • 613
    • 352
    • 91616


    At least with this logic each line would have equal amount of items and would "make sense" that way. I guess the hardest part here is that some of the values are aligned left in their column while most are aligned right.

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2010
    Posts
    38

    Re: Help for deleting "wrong" lines from a file

    Quote Originally Posted by Merri View Post
    The % is apparently printed to denote an overflow of one character. This then further lenghthens the column by one character.

    This is how I understand it:
    09407 13 26 12 485 -3 -395 -842 3178 1625 316700%1663%1177639711613352 91616
    Correctly parsed:
    • 09407
    • 13
    • 26
    • 485
    • -3
    • -395
    • -842
    • 3178
    • 1625
    • 316700
    • 1663
    • 1177
    • 639
    • 711
    • 613
    • 352
    • 91616


    At least with this logic each line would have equal amount of items and would "make sense" that way. I guess the hardest part here is that some of the values are aligned left in their column while most are aligned right.
    You are right the text i am looking is created from a qbasic code using the print using command, so if the length of a value is bigger than the characters gives this error (%) or there is no space between the different variables in the second case i highlight in values. But in any case i don't need those values as they are outside the range of values that i should have, that's why i am trying to create a code where i 'll put the "right" values in a new files and the deleted in another so as to examine them later.

  4. #4
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Help for deleting "wrong" lines from a file

    Hmm, do you also know the code you use to read the data in QBasic? It may very well work with VB simply by using it. I haven't used a whole lot of these "old school" file handling mechanisms, and considering how backwards compatible VB is with earlier Microsoft Basics you may not need to change a lot to get it working.


    Edit!
    Here is some test code anyway, probably doing it a bit silly way, but it works:
    Code:
    Option Explicit
    
    Public Function ParseRow(Row As String, ParamArray ColLength()) As String()
        Dim lngA As Long, lngPos As Long, lngLen() As Long, lngLength As Long, strOut() As String
        ' must have atleast one length parameter
        If UBound(ColLength) >= 0 Then
            ReDim lngLen(UBound(ColLength))
            ReDim strOut(UBound(ColLength))
        Else
            Exit Function
        End If
        ' calculate lengths and total length
        For lngA = 0 To UBound(ColLength)
            lngLen(lngA) = Val(ColLength(lngA))
            lngLength = lngLength + lngLen(lngA)
        Next lngA
        ' can we read the row?
        If Len(Row) >= lngLength Then
            For lngA = 0 To UBound(ColLength)
                ' increase string length for each % character found
                Do While AscW(Mid$(Row, lngPos + 1, 1)) = 37
                    lngLen(lngA) = lngLen(lngA) + 1
                    lngPos = lngPos + 1
                Loop
                ' get the stuff
                strOut(lngA) = Trim$(Mid$(Row, lngPos + 1, lngLen(lngA)))
                ' next please!
                lngPos = lngPos + lngLen(lngA)
            Next lngA
            ' output result
            ParseRow = strOut
        End If
    End Function
    
    Private Sub Form_Load()
        Dim lngA As Long, strData As String, strRow() As String, strRows() As String
        
        strData = _
            "19997 29 36  3   162  -210  -499 -1018  3420  1770  430875  4  4  2  1  5  3  3201" & vbNewLine & _
            "20097 29 48  2   182  -212  -510 -1003  3390  1748  246757 32  4 13 18 26 21254621" & vbNewLine & _
            "00792 11 9   5   -53  -356  -560 -1096  3454  1786  924438  2  2  2  3 10  6 10362" & vbNewLine & _
            "09407 13 26 12   485    -3  -395  -842  3178  1625  316700%1663%1177639711613352 91616" & vbNewLine & _
            "20197 30 45  2   163  -211  -498 -1018  3419  1768  431720  2  3  3  4  9  5  5727" & vbNewLine & _
            "20297 30 33  1   146  -218  -504 -1016  3398  1762  417309  0  0  0  0  0  0     0"
        
        strRows = Split(strData, vbNewLine)
        
        For lngA = 0 To UBound(strRows)
            strRow = ParseRow(strRows(lngA), 5, 3, 3, 3, 6, 6, 6, 6, 6, 6, 8, 3, 3, 3, 3, 3, 3, 6)
            Debug.Print Join(strRow, ",")
        Next lngA
    End Sub
    Last edited by Merri; Apr 26th, 2010 at 06:04 AM.

  5. #5

    Thread Starter
    Member
    Join Date
    Apr 2010
    Posts
    38

    Re: Help for deleting "wrong" lines from a file

    Unfortunately i can't access - change the qbasic code since it is the routine for communicating an instrument with pc and store the data, otherwise it would be very easy to change the print format in the qbasic code

  6. #6
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Help for deleting "wrong" lines from a file

    Ah, I was just a bit too slow: there is a code in the last post that I just added in after your post.

  7. #7
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Help for deleting "wrong" lines from a file

    There is a better way to do what you want but for simple fix of your code:
    Instead of
    InStr(b$, "") = 0
    Change it to
    Left(b$, 1) <> " "

    Edit: I didn't see Merri's code when posting
    Last edited by anhn; Apr 26th, 2010 at 06:28 AM.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  8. #8

    Thread Starter
    Member
    Join Date
    Apr 2010
    Posts
    38

    Re: Help for deleting "wrong" lines from a file

    Quote Originally Posted by Merri View Post
    Ah, I was just a bit too slow: there is a code in the last post that I just added in after your post.
    thanks a lot for your time and your help, it works, i just need to modify a little the code so as to get the input data from a txt file.

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