Jan 4th, 2007, 09:51 PM
#1
Thread Starter
New Member
[RESOLVED] Help parsing a non-delimited text file
I have a text file that I need to export into excel or a comma delimited file. The file consists of a solid string of prices consisting of numbers and decimal points. The only way I can think to break it down is by finding the decimal point in the string advancing 2 digits for the cents(the price is not a fixed length throughout the file) parse it and keep repeating. Also the information represents columnar data,(i.e. there are 7 fields so after each 7th price a new line should be started to build the columns required. I'm sure this is quite simple but I've been away from programming too long for it to click. Any help is appreciated!
Attached Files
Jan 4th, 2007, 09:54 PM
#2
Re: Help parsing a non-delimited text file
Are there always 2 digits after the decimal?
Jan 4th, 2007, 10:03 PM
#3
Thread Starter
New Member
Re: Help parsing a non-delimited text file
Yes there are always 2 digits for the cents field. I posted the data file in the original post
Jan 4th, 2007, 10:29 PM
#4
Fanatic Member
Re: Help parsing a non-delimited text file
I looked at the file. What is the record length? 64, 128, 256, other?
If you know this...no problem
Alpha Micro: Alpha Basic, AS400 V5r2, EDI (Trusted Link/ Inovis.com),Access AS/400 via VB6, Qbasic for data conversions. A mix of Hardware too. ASCII Table , New Number to Words/66 digits , AS/400(v5r2) VB6 Viewer/Ask for code(ODBC) ^ What Is Transferring? , Check your Ports #Perfect Passwords , *Slide Bar Example , Logoff, Restart, Shut-Down PC *Keep Form On Top , Opaque Form ^ Create Objects at Run Time @ Check Key Caps Locks # GetTickCount(System Up Time) * Convert text to Excel & Collected Icons + Resize: Form/Text box ^ PC GateWay via Shell $ Drag & Drop Game ! PopUpMenu *Print File/no Open # Timer on Mult Forms ~ Splash & Mult Forms & Lots of Comments + Random/Timer/Guess * Dec >Hex >Oct >Bin % Get MAC (NIC) < saving to Registry > Wookiee Cookies \ BackUpDisk / World Conection SpeedTest $ Glossary Commonly Used Terms # phonetic list @ Detailed Computer Scan
When posting Code, Use tags.. [CODE] *Your Code* [/CODE]
Jan 4th, 2007, 10:34 PM
#5
Thread Starter
New Member
Re: Help parsing a non-delimited text file
The record length is unknown. However the data is taken from a document that is 7 columns across by 150 rows.
Jan 4th, 2007, 10:39 PM
#6
Re: Help parsing a non-delimited text file
I can write a parse routine for it but you'll have to wait a bit because I have to reload Visual Basic back onto my computer.
Unless someone else comes up with the code before then.
Jan 4th, 2007, 10:48 PM
#7
Fanatic Member
Re: Help parsing a non-delimited text file
Is this one row?
3.803.884.004.164.384.454.
This would be 7 colums
Alpha Micro: Alpha Basic, AS400 V5r2, EDI (Trusted Link/ Inovis.com),Access AS/400 via VB6, Qbasic for data conversions. A mix of Hardware too. ASCII Table , New Number to Words/66 digits , AS/400(v5r2) VB6 Viewer/Ask for code(ODBC) ^ What Is Transferring? , Check your Ports #Perfect Passwords , *Slide Bar Example , Logoff, Restart, Shut-Down PC *Keep Form On Top , Opaque Form ^ Create Objects at Run Time @ Check Key Caps Locks # GetTickCount(System Up Time) * Convert text to Excel & Collected Icons + Resize: Form/Text box ^ PC GateWay via Shell $ Drag & Drop Game ! PopUpMenu *Print File/no Open # Timer on Mult Forms ~ Splash & Mult Forms & Lots of Comments + Random/Timer/Guess * Dec >Hex >Oct >Bin % Get MAC (NIC) < saving to Registry > Wookiee Cookies \ BackUpDisk / World Conection SpeedTest $ Glossary Commonly Used Terms # phonetic list @ Detailed Computer Scan
When posting Code, Use tags.. [CODE] *Your Code* [/CODE]
Jan 4th, 2007, 10:50 PM
#8
Fanatic Member
Re: Help parsing a non-delimited text file
maybe
3.80|3.88|4.00|4.16|4.38|4.45|4.
Jan 4th, 2007, 11:02 PM
#9
Thread Starter
New Member
Re: Help parsing a non-delimited text file
Sessi4ml,
your data line;
3.80|3.88|4.00|4.16|4.38|4.45|4.
is correct except for the last column(7). It should read
4.56 instead of (4.). Then the next data (3.84) goes back to column 1 and loops like this until done.
3.84|4.07|4.47|4.54|4.88|5.00|5.30
Jan 4th, 2007, 11:11 PM
#10
Re: Help parsing a non-delimited text file
prepend "00" to string
split on "." and store in string array
resize a dynamic destination currency array to size ubound(string array) + Iff(len(last string array element)> 2, 1, 0) -1
loop through string array from lbound to ubound - 1
Mid(current string array element, 3) contains whole number, convert to currency and assign to currency array. BTW Mid(str, 3) is reason for prepend of "00"
Left(next array element, 2) contains decimal portion. Convert to num and divide by 100 then add to whole number in currency array.
Special handling for last array element, if len > 2 then theres still one more whole number without decimal point
EDIT: Based this post on 3.80|3.88|4.00|4.16|4.38|4.45|4If decimal points are complete then no special handling or last array element needed. And currency array size is ubound string array - 1
Last edited by leinad31; Jan 4th, 2007 at 11:16 PM .
Jan 4th, 2007, 11:29 PM
#11
Re: Help parsing a non-delimited text file
I think I have an idea. How about a generic function for this kind of job?
VB Code:
' in a module
Option Explicit
Public Function Clip(ByRef Text As String, ByRef Keyword As String, ByVal CharPadding As Byte) As String()
Static lngEnd() As Long, strOut() As String
Dim lngEndUB As Long, lngPos As Long, lngCount As Long
' error detection
If LenB(Text) = 0 Or LenB(Keyword) = 0 Then Exit Function
' reserve space
lngEndUB = 40
ReDim Preserve lngEnd(lngEndUB)
' find first
lngPos = InStr(Text, Keyword)
Do Until lngPos < 1
' correct end position
lngPos = lngPos + CharPadding
' need more space?
If lngEndUB < lngCount Then
lngEndUB = lngEndUB * 2
ReDim Preserve lngEnd(lngEndUB)
End If
' remember
lngEnd(lngCount) = lngPos
' jump to next
lngCount = lngCount + 1
' find next
lngPos = InStr(lngPos + 1, Text, Keyword)
Loop
' decrease as we have one too many
lngCount = lngCount - 1
' none?
If lngCount < 0 Then Exit Function
' reserve space for strings
ReDim Preserve strOut(lngCount)
' validate last item is not out of string length
If lngEnd(lngCount) > Len(Text) Then lngEnd(lngCount) = Len(Text)
' get first item
strOut(0) = Left$(Text, lngEnd(0))
' get remaining items
For lngPos = 1 To lngCount
' clip the one we need
strOut(lngPos) = Mid$(Text, lngEnd(lngPos - 1) + 1, lngEnd(lngPos) - lngEnd(lngPos - 1))
Next lngPos
' return the final string array
Clip = strOut
End Function
VB Code:
' sample project: add listbox and command button
Private Sub Command1_Click()
Dim strTest As String, strIn() As String, lngA As Long
strTest = "3.803.884.004.164.384.45"
strIn = Clip(strTest, ".", 2)
For lngA = 0 To UBound(strIn)
List1.AddItem strIn(lngA)
Next lngA
End Sub
Jan 4th, 2007, 11:35 PM
#12
Re: Help parsing a non-delimited text file
I came up with a quick sub, didn't test it that much but I think it's working. (Cross-checking with the data file is hard on the eyes )
VB Code:
Private Sub LoadData(ByVal FilePath As String, LVObject As Object, ByVal Columns As Integer)
Dim lonLoop As Long 'Loop counter.
Dim intCol As Integer 'Current column we're on.
Dim lonPos As Long 'Current position of decimal.
Dim lonPrevDotPos As Long 'Previous position of decimal.
Dim lonNextDotPos As Long 'Decimal position after current decimal.
Dim intFF As Integer 'File handle.
Dim strData As String 'File data.
Dim intRow As Integer 'Current row.
Dim intPrevRow As Integer 'Previous row.
Dim strCur As String
Dim lonStart As Long
Dim lonEnd As Long
intFF = FreeFile
Open FilePath For Binary Access Read As #intFF
If LOF(intFF) = 0 Then
Close #intFF
Exit Sub
Else
strData = Space$(LOF(intFF))
Get #intFF, 1, strData
End If
Close #intFF
intRow = 1
lonPos = InStr(1, strData, ".", vbBinaryCompare)
With LVObject
.ListItems.Clear
'Keep looping while there are still decimals.
Do While lonPos > 0
'Increase current column.
intCol = intCol + 1
If intCol > Columns Then
intPrevRow = intRow
intRow = intRow + 1
intCol = 1
End If
If lonPrevDotPos = 0 Then
'We're on the first decimal.
'Get current value.
strCur = Mid$(strData, 1, lonPos + 2)
lonPrevDotPos = lonPos
lonPos = lonPos + 2
Else
'This is not the first decimal.
'Get text from previous decimal + 3
'to this decimal + 2
lonStart = lonPrevDotPos + 3
lonEnd = lonPos + 2
'strCur = Mid$(strData, lonPrevDotPos + 3, (lonPos + 2) + (lonPrevDotPos + 3) - 2)
strCur = Mid$(strData, lonStart, (lonEnd - lonStart) + 1)
lonPrevDotPos = lonPos
lonPos = lonPos + 2
End If
If Len(strCur) > 0 Then
If intCol = 1 Then
.ListItems.Add , , strCur
Else
.ListItems(intRow).ListSubItems.Add , , strCur
End If
End If
lonPos = InStr(lonPos + 1, strData, ".", vbBinaryCompare)
Loop
End With
strData = ""
End Sub
Private Sub Command1_Click()
LoadData App.Path & "\data.txt", lvData, 7
End Sub
I also attached a sample project.
Last edited by DigiRev; Mar 26th, 2007 at 02:55 AM .
Jan 4th, 2007, 11:37 PM
#13
Jan 4th, 2007, 11:42 PM
#14
Fanatic Member
Re: Help parsing a non-delimited text file
Maybe I am wrong, or maybe old school
VB Code:
Dim strData As String
'3.80|3.88|4.00|4.16|4.38|4.45|4.
'is correct except for the last column(7). It should read
'4.56 instead of (4.). Then the next data (3.84) goes back to column 1 and loops like this until done.
'3.84|4.07|4.47|4.54|4.88|5.00|5.30
Private Sub Form_Load()
Dim inrStart As Integer: Dim intX As Integer
strData = "3.803.884.004.164.384.454.563.844.074.474.544.885.005.303.874.264.704.855.225.395.883.984.414.965.175.525.706.264.254.495.205.415.776.016.644.324.685.295.585.966.256.874.574.825.425."
intStart = 1
For intStart = 1 To Len(strData)
intX = InStr(intStart, strData, ".")
' x points to the dot
'start+ dot +2..1+2+2 .. start + x +2
Text1.Text = Text1.Text + Mid(strData, intStart, intX + 3 - intStart) + ","
a = a + 1: If a = 7 Then a = 0: Text1.Text = Text1.Text + vbCrLf
intStart = intX + 2
Next
End Sub
My results:
3.80,3.88,4.00,4.16,4.38,4.45,4.56,
3.84,4.07,4.47,4.54,4.88,5.00,5.30,
3.87,4.26,4.70,4.85,5.22,5.39,5.88,
3.98,4.41,4.96,5.17,5.52,5.70,6.26,
4.25,4.49,5.20,5.41,5.77,6.01,6.64,
4.32,4.68,5.29,5.58,5.96,6.25,6.87,
4.57,4.82,5.42,5.,
Jan 4th, 2007, 11:44 PM
#15
Re: Help parsing a non-delimited text file
Ooops by mistake, thought it was data needed to be converted to numeric for immediate use...
If your just gonna format the string to include comma separators...
VB Code:
Option Explicit
Private Sub Form_Load()
MsgBox Parse("3.803.884.004.164.384.454.563.844.074.474.544.885.005.303.874.264.704.855.225.395.883.98")
End Sub
Public Function Parse(ByVal SrcString As String) As String
Dim sarTmp() As String
Dim cnt As Long
sarTmp = Split(SrcString, ".")
For cnt = 1 To UBound(sarTmp) - 1
sarTmp(cnt) = Left(sarTmp(cnt), 2) & "," & Mid(sarTmp(cnt), 3)
Next
Parse = Join(sarTmp, ".")
End Function
Jan 4th, 2007, 11:46 PM
#16
Re: Help parsing a non-delimited text file
Originally Posted by
sessi4ml
Maybe I am wrong, or maybe old school
VB Code:
Dim strData As String
'3.80|3.88|4.00|4.16|4.38|4.45|4.
'is correct except for the last column(7). It should read
'4.56 instead of (4.). Then the next data (3.84) goes back to column 1 and loops like this until done.
'3.84|4.07|4.47|4.54|4.88|5.00|5.30
Private Sub Form_Load()
Dim inrStart As Integer: Dim intX As Integer
strData = "3.803.884.004.164.384.454.563.844.074.474.544.885.005.303.874.264.704.855.225.395.883.984.414.965.175.525.706.264.254.495.205.415.776.016.644.324.685.295.585.966.256.874.574.825.425."
intStart = 1
For intStart = 1 To Len(strData)
intX = InStr(intStart, strData, ".")
' x points to the dot
'start+ dot +2..1+2+2 .. start + x +2
Text1.Text = Text1.Text + Mid(strData, intStart, intX + 3 - intStart) + ","
a = a + 1: If a = 7 Then a = 0: Text1.Text = Text1.Text + vbCrLf
intStart = intX + 2
Next
End Sub
My results:
Don't do concatentaion for the entire stream... that's slow cause memory has to be repeatedly set aside and assigned for growing stream.
Jan 4th, 2007, 11:53 PM
#17
Fanatic Member
Re: Help parsing a non-delimited text file
Don't do concatentaion for the entire stream... that's slow cause memory has to be repeatedly set aside and assigned for growing stream.
True, looking at a 5k file , and this is version 1, and I am new this VB stuff
next time I'll be better
Alpha Micro: Alpha Basic, AS400 V5r2, EDI (Trusted Link/ Inovis.com),Access AS/400 via VB6, Qbasic for data conversions. A mix of Hardware too. ASCII Table , New Number to Words/66 digits , AS/400(v5r2) VB6 Viewer/Ask for code(ODBC) ^ What Is Transferring? , Check your Ports #Perfect Passwords , *Slide Bar Example , Logoff, Restart, Shut-Down PC *Keep Form On Top , Opaque Form ^ Create Objects at Run Time @ Check Key Caps Locks # GetTickCount(System Up Time) * Convert text to Excel & Collected Icons + Resize: Form/Text box ^ PC GateWay via Shell $ Drag & Drop Game ! PopUpMenu *Print File/no Open # Timer on Mult Forms ~ Splash & Mult Forms & Lots of Comments + Random/Timer/Guess * Dec >Hex >Oct >Bin % Get MAC (NIC) < saving to Registry > Wookiee Cookies \ BackUpDisk / World Conection SpeedTest $ Glossary Commonly Used Terms # phonetic list @ Detailed Computer Scan
When posting Code, Use tags.. [CODE] *Your Code* [/CODE]
Jan 4th, 2007, 11:59 PM
#18
Re: Help parsing a non-delimited text file
Originally Posted by
DigiRev
You forgot to mention I coded it in half the time as well!
However, I didn't provide a fully ready to go solution, but hopefully it is a function that'll be of value for atleast someone else reading this thread later on.
Jan 5th, 2007, 12:05 AM
#19
Re: Help parsing a non-delimited text file
Originally Posted by
Merri
You forgot to mention I coded it in half the time as well!
Yea, well half that time I was re-installing VB.
Jan 6th, 2007, 02:04 PM
#20
Fanatic Member
Help parsing a non-delimited text file
Here is a complete solution.
Any questions, just ask.
Alpha Micro: Alpha Basic, AS400 V5r2, EDI (Trusted Link/ Inovis.com),Access AS/400 via VB6, Qbasic for data conversions. A mix of Hardware too. ASCII Table , New Number to Words/66 digits , AS/400(v5r2) VB6 Viewer/Ask for code(ODBC) ^ What Is Transferring? , Check your Ports #Perfect Passwords , *Slide Bar Example , Logoff, Restart, Shut-Down PC *Keep Form On Top , Opaque Form ^ Create Objects at Run Time @ Check Key Caps Locks # GetTickCount(System Up Time) * Convert text to Excel & Collected Icons + Resize: Form/Text box ^ PC GateWay via Shell $ Drag & Drop Game ! PopUpMenu *Print File/no Open # Timer on Mult Forms ~ Splash & Mult Forms & Lots of Comments + Random/Timer/Guess * Dec >Hex >Oct >Bin % Get MAC (NIC) < saving to Registry > Wookiee Cookies \ BackUpDisk / World Conection SpeedTest $ Glossary Commonly Used Terms # phonetic list @ Detailed Computer Scan
When posting Code, Use tags.. [CODE] *Your Code* [/CODE]
Jan 7th, 2007, 11:32 PM
#21
Fanatic Member
Re: [RESOLVED] Help parsing a non-delimited text file
Scooby, here is the Text to Excel.
Good to hear the code worked
Any questions, just ask.
Alpha Micro: Alpha Basic, AS400 V5r2, EDI (Trusted Link/ Inovis.com),Access AS/400 via VB6, Qbasic for data conversions. A mix of Hardware too. ASCII Table , New Number to Words/66 digits , AS/400(v5r2) VB6 Viewer/Ask for code(ODBC) ^ What Is Transferring? , Check your Ports #Perfect Passwords , *Slide Bar Example , Logoff, Restart, Shut-Down PC *Keep Form On Top , Opaque Form ^ Create Objects at Run Time @ Check Key Caps Locks # GetTickCount(System Up Time) * Convert text to Excel & Collected Icons + Resize: Form/Text box ^ PC GateWay via Shell $ Drag & Drop Game ! PopUpMenu *Print File/no Open # Timer on Mult Forms ~ Splash & Mult Forms & Lots of Comments + Random/Timer/Guess * Dec >Hex >Oct >Bin % Get MAC (NIC) < saving to Registry > Wookiee Cookies \ BackUpDisk / World Conection SpeedTest $ Glossary Commonly Used Terms # phonetic list @ Detailed Computer Scan
When posting Code, Use tags.. [CODE] *Your Code* [/CODE]
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