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