Fast Line Parser

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
User avatar
DDastardly71
Coder
Posts: 22
Joined: Fri Oct 17, 2008 6:56 pm
Location: U.S.A.

Fast Line Parser

Post 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?
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post 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
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
User avatar
DDastardly71
Coder
Posts: 22
Joined: Fri Oct 17, 2008 6:56 pm
Location: U.S.A.

Post by DDastardly71 »

Thanks for the code...exactly what I was looking for...short and sweet.

Thanks
Post Reply