October 30, 2025, 10:22:41 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Parsing a line from a file.

Started by Rock Ridge Farm (Larry), May 30, 2008, 07:20:11 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Rock Ridge Farm (Larry)

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?

Quentin

you can use ";" instead of ",". I think this should work too for csv files.

Rock Ridge Farm (Larry)

Problem is that '|' is the separator and I still need a method to parse the file.

Quentin

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.

LarryMc

May 30, 2008, 09:22:32 AM #4 Last Edit: May 30, 2008, 10:53:13 AM by Larry McCaughn
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
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

JoaoAfonso

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.
JoÃÆ'ƒÂÃ,£o Afonso
Viriato
-----------------
Iberia MUD
www.iberiamud.com
iberiamud.com:5900

Jerry Muelver

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


aurelCB


Jerry Muelver

Thank you. You can find a couple more string-manipulation routines in the ibHash demo, including substring extraction by delimiters, and search-replace.

aurelCB

This is differant from i do in Abasic i will try and thanks for link.
zlatko

Rock Ridge Farm (Larry)

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.

Rock Ridge Farm (Larry)

Never mind - I found another copy.