@ =PRoF=:
Thanks for the vote of confidence.
@ TheComet:
He didn't really have a problem he was just showing newbies. Your second code snip doesn't quite work but here's a fix:
a$=GetWord("hello world you suck",2)
print a$
wait key
end
rem returns a word from a string. If you want to get the second word from a line, call word$=GetWord("hello world you suck",2)
function GetWord(Tex$,pos)
` Add a space to beginning and end of the string
Tex$=" "+Tex$+" "
rem find start position of word
letr=0:wrd=0
repeat
inc letr
if mid$(Tex$,letr)=" " then inc wrd
until wrd=pos
rem find end of word
` wrd isn't needed anymore so it's reused so letr is kept
wrd=letr
repeat
inc wrd
until mid$(Tex$,wrd)=" "
Tex$=left$(right$(Tex$,len(Tex$)-letr),wrd-letr-1)
endfunction Tex$
The only thing that shouldn't be done was using the variable name of string. It's best not to use variables that are exactly the same as keywords... it may lead to weird errors.
That's a nice way of doing it. I would of used one FOR/NEXT loop like this:
a$=GetWord2("hello world you suck",2)
print a$
wait key
end
function GetWord2(Tex$,pos)
` Add a space to begining and end of the string
Tex$=" "+Tex$+" "
` Go through all the characters of Tex$
for t=1 to len(Tex$)
` Check for a space
if mid$(Tex$,t)=" "
` Increase the number of words found
inc Words
` Check if the WordStart was found already
if WordStart>0
` Set the WordEnd
WordEnd=t-WordStart
` Leave the FOR/NEXT loop
exit
endif
` Check if the right word number is found and set WordStart
if Words=pos then WordStart=t+1
endif
next t
` Grab the word
Tex$=mid$(Tex$,WordStart,WordEnd)
endfunction Tex$
But the less code the better. IanM will always beats us.
a$=GetWord3("hello world you suck",2)
print a$
wait key
end
` Get a word using IanMs Matrix 1 Utilities Plugin
function GetWord3(Tex$,pos)
` Split words by spaces
split string Tex$," "
` Grab word number pos
Tex$=get split word$(pos)
endfunction Tex$