Page 1 of 1

Fast Line Parser

Posted: Fri Jan 16, 2009 2:58 am
by DDastardly71
Anybody have a fast code for parsing a line of text? I have one but it takes about 5 seconds to process 65,000 lines of text. I don't exactly need to extract the word just return the pointer of the first character in the text.

Something like this...

a$ = "the quick brown fox jumps over the lazy dog"

DO
ParseLine a$, Position, Length
IF Length > 0 THEN
PRINT MID$(a$, Position, Length)
ELSE
EXIT DO
END IF
LOOP

The result I'm looking for are these for a total of 9 loops.

1 3 the <-- position=1, length=3, word=the
5 5 quick <-- position=5, length=5, word=quick
.
.
41 3 dog <-- position=41, length=3, word=dog

Is there another way of parsing the line without examining every single character in the string?

Posted: Fri Jan 16, 2009 1:16 pm
by burger2227
You could try using INSTR() for the line spaces as below:

Code: Select all

text$ = "the quick brown fox jumps over the lazy dog"
start = 1
DO
spaces = INSTR(start, text$, " ") ' in a loop 
'figure the word parameters 
position = start
IF spaces = 0 THEN 
    length = LEN(text$) - lastword
    word$ = MID$(text$, lastword + 1, length) 
ELSE
    length = spaces - start 
    word$ = MID$(text$, start, length)
    'set for next space search
    start = spaces + 1: lastword = spaces
END IF
PRINT "Pos ="; position; "Length ="; length; "word = "; word$
LOOP UNTIL spaces = 0

You can find the end of each word by the space position - 1 until the last return is 0. The last word is just the last space position + 1 up to the length of the sentence. Trim the sentences for no leading or trailing spaces. INSTR is often used to find the start position of a word search.
It can be used several times by incrementing the start position.

Ted

Posted: Sun Jan 18, 2009 1:12 am
by DDastardly71
Thanks for the code...exactly what I was looking for...short and sweet.

Thanks