|
-
Apr 26th, 2010, 05:19 AM
#1
Thread Starter
Member
[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.
-
Apr 26th, 2010, 05:38 AM
#2
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.
-
Apr 26th, 2010, 05:46 AM
#3
Thread Starter
Member
Re: Help for deleting "wrong" lines from a file
 Originally Posted by Merri
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.
-
Apr 26th, 2010, 05:57 AM
#4
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.
-
Apr 26th, 2010, 06:03 AM
#5
Thread Starter
Member
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
-
Apr 26th, 2010, 06:08 AM
#6
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.
-
Apr 26th, 2010, 06:19 AM
#7
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.
-
Apr 26th, 2010, 06:20 AM
#8
Thread Starter
Member
Re: Help for deleting "wrong" lines from a file
 Originally Posted by Merri
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|