Quote:
|
Originally Posted by Stefan
Well I could imagine that it could be useful for some cases to get an array of the separate tokens plus the delimiters, if you have more than one delimiter and want to restore the original string afterwards. The tokenizeWithDelims function should add to the temp.list though (use it as array) and not append the string with comma etc.
|
yea, i'm just being lazy because I counting the number of tokens would be too much work, lol. Although sure, I'll do it. Still would be better built into tokenize somehow though. Maybe you could make an optional parameter. It's most useful function though, would be if you wanted to separate different kinds of data and use the delimiter as functionality to that data. This example wouldn't be good in graal, but let's say you had
PHP Code:
string="/deposit 20g";
tokens = string.tokenizewithdelims("/");
if (tokens[0] == "/") {
switch (tokens[1].trim()) {
case "deposit":
moretokens = tokens[2].tokenizewithdelims("gt$");
if (moretokens[0].type() == int) {
switch (moretokens[1]) {
case "g": depositingelat(moretokens[0]); break;
case "t": depositintickets(moretokens[0]); break;
case "$": depositindollars(moretokens[0]); break;
}
case "withdraw": blah; break;
case "transfer": blah; break;
}
}
probably not the best example. a better one might be
PHP Code:
string="2+2";
string.tokenizewithdelims(+-*/);
switch (tokens[1]) {
case "+": return tokens[0]+tokens[2];
case "-": return tokens[0]-tokens[2];
case "*": return tokens[0]*tokens[2];
case "/": return tokens[0]/tokens[2];
}
well yea, the .trim() in the first one kindof makes it seem to me now, that the best way to put this functionality into gscript might be
string.tokenize(ignoreddelims,returneddelims);
where string.tokenize() is an overloaded function wherever it comes from in the script.
Would also be cool if you could base tokens off of strings, but it's easier to get around that than with single character delims.