Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

split string with no delimiters?

gene Poole
"Foolish humans!"
Join date: 16 Jun 2004
Posts: 324
01-17-2005 11:07
string test = "test";
list chars = llParseString2List(test, [], []);

I wish 'chars' would now look something like ["t", "e", "s", "t"]. Perhaps surprisingly, it does not ('chars' ends up empty).

Any way to make this work? I just wanted a nicer way to do it than a 'for' loop that gets the 1-char substring of a rolling index and adds it to the list.
Moleculor Satyr
Fireflies!
Join date: 5 Jan 2004
Posts: 2,650
01-17-2005 11:45
Try [""] for your second argument in llParseString2List.

Dunno if it'll work. There are other, kludgier ways to do what you want, but you can always try that first.
_____________________
</sarcasm>
Tread Whiplash
Crazy Crafter
Join date: 25 Dec 2004
Posts: 291
Use 'Spacers' Parameter, or llGetSubString()....
01-17-2005 13:19
The simple (albeit "long-hand";) way to do this, is to use the last parameter of the llParseString2List() function. This is called "spacers", and it seperates and KEEPS each "spacer" character as an entry in the final list. If you want to seperate on EVERY letter, you just specify them all in the spacer param!

So, your call should look something like this:

CODE

list spacers = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r",
"s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M",
"N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];

//Note that the above list doesn't include numbers or other symbols.

list myList = llParseString2List(someString,[],spacers);

and voila!

OR, if you want a list with one entry for EACH character in a string, no matter what it is, you could:

CODE

integer stringLen = llStringLength(myString);
integer i;

list myList;

//Repeatedly concatenating the list might be very slow,
// because LSL is pass-by-value only. *sigh*
for(i = 0; i < stringLen; i++)
{
myList += [llGetSubString(myString, i, i)];
}


Enjoy!

Take care,

--Noel "HB" Wade
(Tread Whiplash)
gene Poole
"Foolish humans!"
Join date: 16 Jun 2004
Posts: 324
01-18-2005 23:56
From: Moleculor Satyr
Try [""] for your second argument in llParseString2List.

Dunno if it'll work. There are other, kludgier ways to do what you want, but you can always try that first.


Tried that; no dice. :(

From: Tread Whiplash
The simple (albeit "long-hand";) way to do this, is to use the last parameter of the llParseString2List() function. This is called "spacers", and it seperates and KEEPS each "spacer" character as an entry in the final list. If you want to seperate on EVERY letter, you just specify them all in the spacer param!


That idea is equally parts brilliant and disgusting, rather like the char-to-ordinal conversion "trick". :D

From: Tread Whiplash

OR, if you want a list with one entry for EACH character in a string, no matter what it is, you could:

CODE

integer stringLen = llStringLength(myString);
integer i;


list myList;

//Repeatedly concatenating the list might be very slow,
// because LSL is pass-by-value only. *sigh*
for(i = 0; i < stringLen; i++)
{
myList += [llGetSubString(myString, i, i)];
}


Enjoy!

Take care,

--Noel "HB" Wade
(Tread Whiplash)


*Sigh* indeed! I could use that one as a substitute for llSleep(). :p But again, it would work.

Thanks for the thoughts. Cheers! :)
Christopher Omega
Oxymoron
Join date: 28 Mar 2003
Posts: 1,828
01-19-2005 10:21
Try llParseStringKeepNulls(yourString, [""], []);
That might work, as llParseString2List may ignore "".
==Chris
Tread Whiplash
Crazy Crafter
Join date: 25 Dec 2004
Posts: 291
Lsl....
01-19-2005 12:15
Gene -

Hey, its an interpretted language. And Lists have high overhead and there's no alternative to them. Wish I had a better solution for you - but some things just aren't easy to do in LSL. I think you have found an excellent one. :p

Yes, the "long-hand" method I mentioned is gross in some ways - but it would be a heck of a lot faster than the second method; which is why I explained them both. They each have their plusses & minuses.

Really, to be honest, I gotta say that if its speed you're looking for, you should maybe think about a different way to store / code your data. Parsing a string down to each individual character is a lot of "work", no matter which way you slice it. Do you really need to capture every single character? You can't use numbers as "codes" (instead of long strings) and equate them to global vars (pseudo-constants) you've pre-defined? Or some other alternative? In a limited programming environment like LSL, sometimes you have to trade complex elegance for simpler "stock" code-paths (like replacing a complex-but-elegant calculation of values on the fly with a limited number of pre-defined values); or make other design trade-offs so that your system will work with the given resources.

Take care,

--Noel "HB" Wade
(Tread Whiplash)
Tiger Crossing
The Prim Maker
Join date: 18 Aug 2003
Posts: 1,560
01-19-2005 12:34
As a speed test, compare...

A) string to list character-by-character
and
B) string to string+comma character-by-character, then llCSV2List

...and see which is faster. I bet B is.
_____________________
~ Tiger Crossing
~ (Nonsanity)
gene Poole
"Foolish humans!"
Join date: 16 Jun 2004
Posts: 324
01-19-2005 22:44
Well, here's the deal:

I'm using a string to represent a 2d structure in coded form, and I wanted to "scramble" the code, so I thought I might be able to:
CODE

list bar = llParseString2List(foo, [], []); // if it worked like this
bar = llListRandomize(bar, 1);
foo = llDumpList2String(bar, "");

but really it's about laziness (and code clarity, of course!). ;)

In any case, I've discovered, using the magic of algebra, that some permutations will yield unsuitable results, so I've opted for a slightly more involved way of doing the scrambling.

But thanks to everyone for the all comments/ideas/insights.

Chris, I'll try that, and if it works, Wiki update time.

Tiger, I might try that, but until I do, I'll put my money with yours.