This just splits a string up into pieces. It gets split every time it comes across the character that you choose to split it with.
Here's the code:
`Setup
sync on : sync rate 60
set display mode 1024,768,32
`Generate string
gen:
num=0
st$=""
sp$=","
for l=1 to 10+rnd(20)
num=1-num
if num=1
st$=st$+str$(rnd(20))
else
st$=st$+sp$
endif
next l
`Main loop
do
`Tokenize string
text 10,10,"String: "+st$
text 10,25,"-----------------"
for t=1 to getTokens(st$,sp$)
text 10,30+(20*t),getToken(st$,sp$,t)
next t
`Restart
text 10,screen height()-30,"Press space to restart"
if spacekey() then goto gen
sync
cls
loop
`Functions
function getTokens(st$,sp$)
repeat
counter=counter+1
cur$=mid$(st$,counter)
if cur$=sp$ then inc splits
until counter>len(st$)
tokens=splits+1
endfunction tokens
function getToken(st$,sp$,num)
nst$=sp$+st$+sp$
repeat
inc counter
cur$=mid$(nst$,counter)
if cur$=sp$ then inc splits
until splits=num or counter>len(st$)
repeat
inc counter
cur$=mid$(nst$,counter)
if cur$<>sp$ then token$=token$+cur$
until cur$=sp$ or counter>len(st$)
endfunction token$
It would be usefull for things like changing dates from 17/11/2006 into seperate things. Also for things such as reading data from a file, for instance:
cube 1,100,500,300
could easily be split up into componenets like object number and position x/y/z.
Very useful me thinks.