Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

split string?

Ludvaig Lindman
Registered User
Join date: 2 Feb 2007
Posts: 78
12-16-2007 07:21
hi,
was wondering if it was possible to split a string into its characters.
Example:

I have a string (word) like "WORD".
Now i want the script to split it into: "W","O","R,"D".

Is this possible?

Thanks in advance.
Siann Beck
Beauty & Braiiiiinsss!
Join date: 14 Jul 2007
Posts: 140
12-16-2007 07:37
list sbSplit(string strWord) {
integer x;
list lstLetters;
integer intLength = llStringLength(strWord);

for (x = 0; x < intLength; x++) {
lstLetters = (lstLetters=[]) + lstLetters + [llGetSubString(strWord, x, x)];
}

return lstLetters;
}
Ludvaig Lindman
Registered User
Join date: 2 Feb 2007
Posts: 78
12-16-2007 08:01
Thanks a lot!
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
12-16-2007 10:20
There is some issue that in the future that the execution order will change and the (lstLetters=[]) hack won't work anymore. The below code does the same action without using it.

CODE
list sbSplit(string strWord) {
integer x = 0;
string t = "";
list lstLetters = [];

while((t = llGetSubString(strWord, --x, x))) lstLetters = t + lstLetters;

return lstLetters;
}


EDIT:
sorry, i don't know why I made my function run backwords. I have fixed it.
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey
Siann Beck
Beauty & Braiiiiinsss!
Join date: 14 Jul 2007
Posts: 140
12-16-2007 12:17
From: Strife Onizuka
There is some issue that in the future that the execution order will change and the (lstLetters=[]) hack won't work anymore.
Breaking countless scripts that use it? That's lovely. Will they at least, at the same time, make lists more efficient so that the hack isn't necessary?
Wyatt Burton
Registered User
Join date: 11 Jan 2007
Posts: 49
12-16-2007 19:51
From: Ludvaig Lindman
hi,
was wondering if it was possible to split a string into its characters.
Example:

I have a string (word) like "WORD".
Now i want the script to split it into: "W","O","R,"D".

Is this possible?

Thanks in advance.


Here is an alternate version

list sbSplit(string strWord)
{
if (llStringLength(strWord)<= 1) {
return [strWord];
}
return [llGetSubString(strWord, 0, 0)]+sbSplit(llGetSubString(strWord, 1, -1));
}
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
12-16-2007 20:06
@Wyatt: You do not want to use recursion in LSL, it's very expensive.

Also, for single list entries you shouldn't wrap it in [], they are costly; you should use the (list) typecast. The + operator handles list addition with non list items so you don't need to even typecast it.

Anyway, why do you want to split the string into a list? In most situations you just want to operator on it char at a time so it's just easier to use the string functions.
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey