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?
Fast Line Parser
- burger2227
- Veteran
- Posts: 2466
- Joined: Mon Aug 21, 2006 12:40 am
- Location: Pittsburgh, PA
You could try using INSTR() for the line spaces as below:
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
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
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
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
- DDastardly71
- Coder
- Posts: 22
- Joined: Fri Oct 17, 2008 6:56 pm
- Location: U.S.A.