mIRC Home    About    Download    Register    News    Help

Print Thread
#241515 01/05/13 08:19 AM
Joined: May 2013
Posts: 2
B
binari Offline OP
Bowl of petunias
OP Offline
Bowl of petunias
B
Joined: May 2013
Posts: 2
Hi.
Is there a simple possibility to split a sentence into words??
Something like that: "today is wednesday" into an array: {"today", "is", "yesterday"}?

Joined: Dec 2008
Posts: 95
A
Babel fish
Offline
Babel fish
A
Joined: Dec 2008
Posts: 95
There are no actual arrays like that in mSL but see /help Token Identifiers, specifically $gettok().

Example:

Php Code:
//var %x = today is wednesday | var %i = 1 | while (%i <= $numtok(%x,32)) { echo -ag token %i -> $gettok(%x,%i,32) | inc %i } 

Quote:
token 1 -> today
token 2 -> is
token 3 -> wednesday



Generally, in mIRC, all texts are tokenized/split by spaces.
So when someone says "today is wednesday" on a channel (/help on TEXT), it'll already be split into $1, $2, and $3, where $1- would be the entire text/all tokens.
Also see /help $1- on that.


So regarding above example, there also is a /tokenize command which is not recommended to use but would work as follows:
Php Code:
//var %x = today is wednesday | tokenize 32 %x | echo -ag $* 

Quote:
today
is
wednesday

Joined: Oct 2004
Posts: 8,330
Hoopy frood
Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,330
I've never heard anyone say that tokenize shouldn't be used. Is there some reason you say it's not recommended? Tokenize works very well for splitting words in locations that are not already split. Maybe you're referring to the $* as I've seen some say that isn't recommended because it's not documented. But that isn't needed when using tokenize. It's just a simple way to display each token on another line once you have the tokens.

To the OP, if you're monitoring what is typed by someone, all you really need to do is use $1, $2, etc. in your ON TEXT event as mentioned in the middle of asdfasdf's post.

Example:
Code:
on *:text:*:#:{
  echo -a Word 1: $1 ~ Word 2: $2 ~ Word 3: $3 ~ All words: $1- ~ All words except the first: $2- ~ Words 2-3: $2-3
}


As you can see, all you need to do is use $ and the word number to get that word. Use - at the end to get that word through the end of the sentence. And to get a range, add the final word number after the -. There are a variety of things you can do, but this can get you started. Use of token identifiers like $gettok() are very useful when you want the "words" to be separated by something other than a space, such as splitting a list of words/phrases at every comma. Tokenize also works for splitting a sentence by other characters. Unlike using $gettok(), the use of tokenize gets you back to using $1, $2, etc. instead of having to use $gettok(sentence, "word" #, chr to split on) every time.


Invision Support
#Invision on irc.irchighway.net
Joined: Dec 2008
Posts: 95
A
Babel fish
Offline
Babel fish
A
Joined: Dec 2008
Posts: 95
Oh, right, when I think of /tokenize I somehow always think of $* in combination with it - I stand corrected. smile

Joined: May 2013
Posts: 2
B
binari Offline OP
Bowl of petunias
OP Offline
Bowl of petunias
B
Joined: May 2013
Posts: 2
Php Code:
//var %x = today is wednesday | var %i = 1 | while (%i <= $numtok(%x,32)) { echo -ag token %i -> $gettok(%x,%i,32) | inc %i } 


That's it. Thanks! It works perfectly smile


Link Copied to Clipboard