tokens are simply a method of breaking things up.
you said ...
"one two three four five" ... i want to write it to the channel ... first character from each word and color it red, then color the remaining characters in the
word blue.
Ok so the tokenizing is really only relevent here in getting each word, so your tokens are words, now if someoen said "whats a word?" you might say "its any text seperated by a space"
So Space character is the token seperator, space characters asc value is 32, so you might see
/set %text one two three four five Bob seven
//echo -a $gettok(%text,2,
32)
two
//echo -a $gettok(%text,6,
32)
Bob
The
32 is the seperator of each token
You might have this
/set %text The Cat sat on the mat. The Dog went to the door. The Bird was in the cage. The Man was in bed.
//echo -a $gettok(%text,3,
46)
The Bird was in the cage
//echo -a - $+ $gettok(%text,2,
46) $+ -
- The Dog went to the door-
//echo -a $gettok(%text,-1,
$asc(.))
The Man was in bed
The
46 is the seperator of each token, 46 is asc for . as you see from the 3rd echo you can use the actual character, and get its asc value, but why bother if u already know it.
The second echo also was designed to show you that the leading space is still in the token, just the other 2 echos destroy it when displaying there results.
You well note that the . is not part of the token , it is considered the seperator so isnt included.
So simply tokens are nothing more than a way to break up a bit of text based apon one of the characters in the text.
There are some things to watch out for
Leading and trailing seperator characters are purged (no mater how many)
/set %text .The Cat sat on the mat. The Dog went to the door. The Bird was in the cage. The Man was in bed.
//echo -a $gettok(%text,
1,46)
The Cat sat on the mat
/set %text .........The Cat sat on the mat. The Dog went to the door. The Bird was in the cage. The Man was in bed..................
//echo -a $gettok(%text,
1,46)
The Cat sat on the mat
//echo -a $numtok(%text,46)
4
//echo -a $gettok(%text,
1-,46)
Cat sat on the mat. The Dog went to the door. The Bird was in the cage. The Man was in bed
Also seperator characters directly following one another become one seperator character.
/set %text The Cat sat on the mat............... The Dog went to the door. The Bird was in the cage. The Man was in bed.
//echo -a $gettok(%text,
1-2,46)
Cat sat on the mat. The Dog went to the door
The effect of erasing or reducing seperators doesnt effect %text of course unless you load the result of the tokening back into it
/set %text The Cat sat on the mat............... The Dog went to the door. The Bird was in the cage. The Man was in bed.
//set %text $gettok(%text,1-,46)
/echo -a %text
The Cat sat on the mat. The Dog went to the door. The Bird was in the cage. The Man was in bed.
The TOKENIZE command works on the same grounds
//tokenize 46 ....The Cat sat on the mat............... The Dog went to the door. The Bird was in the cage. The Man was in bed.... | echo -a $1-
The Cat sat on the mat The Dog went to the door The Bird was in the cage The Man was in bed
//tokenize 46 ....The Cat sat on the mat............... The Dog went to the door. The Bird was in the cage. The Man was in bed.... | echo -a $2
The Dog went to the door
You well note the . is gone altogether now, since $1- well be $1<space>$2<space>$3<space>$4<space>etc etc
Back to what you wanted, heres one way
assume %text = one two three four five
var %i = $numtok(%text,32)
while (%i) {
var %word = $gettok(%text,%i,32)
var %word = 04 $+ $left(%word,1) $+ 12 $+ $mid(%word,2)
var %text = $puttok(%text,%word,%i,32)
dec %i
}
that of course can look like this, but is much harder to read.
var %i = $numtok(%text,32)
while (%i) {
var %text = $puttok(%text,04 $+ $left($gettok(%text,%i,32),1) $+ 12 $+ $mid($gettok(%text,%i,32),2),%i,32)
dec %i
}
Hope this helped