I have an input file that uses '|' as the separator.
I want to parse it for storage in a database..
I thought of converting it to ',' and saving as a .csv but that does not work since
some of the fields contain ',' separated data.
Is there an easy way to do this?
A data line could look like:
aaa|bbb|ccc,ddd,eee,fff|ggg
Any help?
you can use ";" instead of ",". I think this should work too for csv files.
Problem is that '|' is the separator and I still need a method to parse the file.
not sure if I understand the problem.
Just read the file line by line and split the line into the fields
e.g. like this
const false = 0
const true = 1
lin$ = "aaa|bbb|ccc,ddd,eee,fff|ggg"
start = 1
finished = false
openconsole
while finished = false
pos = instr(lin$, "|", start)
field$ = mid$(lin$, start, pos - start)
print field$
start = pos + 1
if pos = 0
finished = true
endif
wend
DO:UNTIL INKEY$ <> ""
closeconsole
end
sorry if I didn't understand the prob.
fletchies ctl.lib has a SplitTwo function that was designed to do exactly what you want to do.
I use it all the time.
Get it here: http://www.codingmonkeys.com/index.php?action=tpmod;dl=item108
Larry
This comes from noteworx program that comes in the IB STD archive, at least: in order to separate data, try to use the character `. it is a `, not a ÂÃ,´ or a ' :) last one is apostrophe, first 2 are accents, just like ~ or ^, characters that almost for sure you wont use alone in the data you want to sabe in your file, so it will be good separators. In noteworx there is also a routine, to prevent wrong data separators, that before write data into file, it checks that string in search for ÂÃ,´ (accent) characters and if any found, it replaces by a ' (apostrophe).
Hope it helps.
Here's a function I've been using since about 1997 in one form or another....
'------------------------------------------------------------------
SUB splitstr(tline as string, splitter as string),HEAP
'splits string tline on delimiter splitter
' returns split-off chunk, modifies (chops)
' original string as well
DEF tok as POINTER
tok = AllocHeap(64000)
DEF tokpos:INT
tline = LTRIM$(tline)
tokpos = INSTR(tline,splitter)
IF(tokpos = 0)
#<STRING>tok = tline
tline = ""
ELSE
#<STRING>tok = LEFT$(tline,tokpos-1)
tline = MID$(tline,tokpos+LEN(splitter))
ENDIF
RETURN #<STRING>tok
ENDSUB
Great code Jerry ;)
Thank you. You can find a couple more string-manipulation routines in the ibHash demo (http://ebasic.wikispaces.com/jlm+ibHash+demo), including substring extraction by delimiters, and search-replace.
This is differant from i do in Abasic i will try and thanks for link.
zlatko
Can someone put ctl.lib on this site or point me to another location.
I can not get into C.M. It keeps making me change my PW and then
will not let me log in.
Never mind - I found another copy.