Don't click here unless you want to be banned.

LSL Wiki : ExampleListConversion

HomePage :: PageIndex :: RecentChanges :: RecentlyCommented :: UserSettings :: You are ia360925.us.archive.org

Example List Conversion


Here's a snippet that converts lists to strings and back while preserving the actual element type. This is useful for passing lists via chat or link messages, etc. Drop it into your code and away you go.

This version uses the newer dump/parse functions. Props to the author of the original which can be found here.

Feel free to use and modify this in your own code for any purpose.

WARNING: Be careful passing floats by themselves or as part of a vector. The way LSL does string conversion on floats can result in inaccuracies at the 6th significant digit. For more info see float.

////////////////////////////////////////////////////////
// snip list convert // ready jack // 11 Nov 2005 // 1.0
string slc_sep = "#!$"; // funky name for clean namespaces

string list_2_string(list l) {     
    list result = [];
    integer len = llGetListLength(l); // optimization
    integer i; 
    for (i = 0; i < len; ++i) {
        result += [llGetListEntryType(l, i)] + llList2List(l, i, i);
    }
    return llDumpList2String(result, slc_sep);
}

list string_2_list(string s) {
    list l = [];
    list result = [];
    l = llParseStringKeepNulls(s, [slc_sep], []);
    integer len = llGetListLength(l);
    integer i;
    for (i = 0; i < len; ++i) {
        integer type = (integer) llList2String(l, i);
        ++i; // CLEVERNESS WARNING...BE VERY AFRAID
        if (type == TYPE_INTEGER) result += (integer)llList2String(l, i);
        else if (type == TYPE_FLOAT) result += (float)llList2String(l, i);
        else if (type == TYPE_KEY) result += (key)llList2String(l, i);
        else if (type == TYPE_VECTOR) result += (vector)llList2String(l, i);
        else if (type == TYPE_ROTATION) result += (rotation)llList2String(l, i);
        else result += llList2String(l, i);
    }
    return result;
}
//////////////////// snip list convert /////////////////


Examples
Comments [Hide comments/form]
This script doesn't seem to retain the exact same values for floats, vectors, and rotations. Is it even possible to do this in LSL? Any ideas? I know this concept as object serialization and deserialization in Java.
-- GordonPrior (2006-01-11 00:56:57)
One minor improvment I can think of is to strip the floats out of vectors and rotations so you at least get that sixth digit that comes with floats. It seems like a lot of work for this script for not much precision improvement. It would be preferable to go all the way of actually converting floats, vectors, and rotations to hexadecimal or some other more compact ASCII encoding than bother with going after the sixth digit in vectors and rotations.
-- GordonPrior (2006-01-11 01:03:39)
Interesting.

I haven't needed that level of precision for my uses. And from a performance standpoint I'd be concerned about the overhead of adding the encoding as these functions are scary as it is. Would be cool to have a second precision version here for people who don't care about performance. Not sure how much it can be helped considering the limitations of the standard float (not a double) used by LSL. See the float page for more info.

A good thing to be forwarned about though.
-- ReadyJack (2006-01-21 19:16:41)
Use Float2Hex to store floats. No decoder is needed, will work with rotations(quaternions) and vectors.
-- BlindWanderer (2006-01-25 03:09:36)
I want to convert string
18,-1,<1,1,1>,1.0
to list
[18,-1,<1,1,1>,1.0]
without modifying the string to be
1`18`1`-1`5`<1,1,1>`2`1.0
with string slc_sep = "`";
so that I can write many lines of data that would be passed to llSetPrimitiveParams function in a human readable format.

If not possible using LSL script, perhaps someone would be willing create a web-based converter? ^_+
-- 99.139.218.108 (2007-12-07 01:39:38)
Attach a comment to this page: