LSL Wiki Mirror 10-5-2006: llCSV2List

HomePage :: PageIndex :: RecentChanges :: RecentlyCommented :: UserSettings ::
list llCSV2List(string src)

Create a list from a string of comma separated values (CSV) specified in src.

All list items will be of type string, regardless of their contents (llGetListEntryType will always return TYPE_STRING). This should be kept in mind if you intend to sort such a list using llListSort or transmit it via llMessageLinked.

llList2CSV performs the inverse function.

One thing to be aware of is that llCSV2List has a built in escaping mechanism. Anything inside angle brackets (like <0, 5, 10>) remains unparsed no matter if it is a vector, rotation or just garbage.

If you need more flexibility, such as separators other than comma, use llParseString2List instead.


Heres an example showing the difference between a list before and after using llCSV2List:

string list2stringtype(list input) { // converts list to a CSV string with type information prepended to each item
    list        output;
    string      s;
    integer     i;
    
    for (i = 0; i < llGetListLength(input); i++) {
        if (llGetListEntryType(input, i) == TYPE_INTEGER) s = "(integer)";
        else if (llGetListEntryType(input, i) == TYPE_FLOAT) s = "(float)";
        else if (llGetListEntryType(input, i) == TYPE_STRING) s = "(string)";
        else if (llGetListEntryType(input, i) == TYPE_KEY) s = "(key)";
        else if (llGetListEntryType(input, i) == TYPE_VECTOR) s = "(vector)";
        else if (llGetListEntryType(input, i) == TYPE_ROTATION) s = "(rotation)";
        else if (llGetListEntryType(input, i) == TYPE_INVALID) s = "(INVALID)";

        output += [s + llList2String(input, i)];
    }
    
    return llList2CSV(output);
}

default {
    state_entry() {
        list        l;

        l = [4, 4.4f, "banana", "15.3", <1,2,3>, <4,5,6,7>]; // this list has proper type information
        llSay(0, list2stringtype(l));
        
        l = llCSV2List(llList2CSV(l));  // this list is converted to a CSV string and back
        llSay(0, list2stringtype(l));   // causing it to loose all type information
    }
}



And here are two functions to safely transport lists through strings (as used in llMessageLinked) while retaining their type information:

string List2TypeCSV(list input) { // converts a list to a CSV string with type information prepended to each item
    integer     i;
    list        output;
    integer     len;

    len=llGetListLength(input); //this can shave seconds off long lists
    for (i = 0; i < len; i++) {
        output += [llGetListEntryType(input, i)] + llList2List(input, i, i);
    }
    
    return llList2CSV(output);
}

list TypeCSV2List(string inputstring) { // converts a CSV string created with List2TypeCSV back to a list with the correct type information
    integer     i;
    list        input;
    list        output;
    integer     len;
    
    input = llCSV2List(inputstring);

    len=llGetListLength(input);
    for (i = 0; i < len; i += 2) {
        if (llList2Integer(input, i) == TYPE_INTEGER) output += (integer)llList2String(input, i + 1);
        else if (llList2Integer(input, i) == TYPE_FLOAT) output += (float)llList2String(input, i + 1);
        else if (llList2Integer(input, i) == TYPE_STRING) output += llList2String(input, i + 1);
        else if (llList2Integer(input, i) == TYPE_KEY) output += (key)llList2String(input, i + 1);
        else if (llList2Integer(input, i) == TYPE_VECTOR) output += (vector)llList2String(input, i + 1);
        else if (llList2Integer(input, i) == TYPE_ROTATION) output += (rotation)llList2String(input, i + 1);
    }
    
    return output;
}

default {
    state_entry() {
        list    l;
        string  s;
        
        l = [4, 4.4, "banana", "15.3", <1, 2, 3>, <4, 5, 6, 7>]; // a list with nice type information

        s = List2TypeCSV(l); // convert to CSV string with type information prepended to each item
        
        l = TypeCSV2List(s); // convert back to list with proper type information
        
        if (llGetListEntryType(l, 5) == TYPE_ROTATION) llSay(0, "Hooray!"); // it worked
    }
}

A variation using the newer dump/parse methods can be found at ExampleListConversion.


Functions | Lists | llList2CSV
There is one comment on this page. [Display comments/form]