ListCast

From Second Life Wiki

Jump to: navigation, search

Template:Needs Translation/LSL/de Template:Needs Translation/LSL/es Template:Needs Translation/LSL/el Template:Needs Translation/LSL/fr Template:Needs Translation/LSL/he Template:Needs Translation/LSL/it Template:Needs Translation/LSL/ja Template:Needs Translation/LSL/ko Template:Needs Translation/LSL/nl Template:Needs Translation/LSL/hu Template:Needs Translation/LSL/no Template:Needs Translation/LSL/da Template:Needs Translation/LSL/sv Template:Needs Translation/LSL/tr Template:Needs Translation/LSL/pl Template:Needs Translation/LSL/pt Template:Needs Translation/LSL/ru Template:Needs Translation/LSL/uk Template:Needs Translation/LSL/zh-Hans Template:Needs Translation/LSL/zh-Hant

Contents

Summary

Function: list listCast( list mixedList, integer type );

Iterates through the contents of a list, producing a resulting list whose contents are cast to a single type. Please refer to llGetListEntryType() for valid types. This function is useful for converting a list to a type which is more memory efficient, or to a type that you know you are going to access a lot (in order to remove extra casting later-on).
Returns a list whose contents are all one type.

• list mixedList The list to cast, its contents may be anything.
• integer type The type of variable to cast all entries to.

Examples

 
        list mixed = [1, 2.0, "3", "Four", (key)"8aef4875-6873-0919-f5ab-c9d6b48d18d4"];
 
        // Prints: 1, 2, 3, 0, 8
        llOwnerSay(llList2CSV(listCast(mixed, TYPE_INTEGER)));
        // Prints: 1.000000, 2.000000, 3.000000, 0.000000, 0.000000
        llOwnerSay(llList2CSV(listCast(mixed, TYPE_FLOAT)));
 

Deep Notes

Implementation

list listCast(list mixedList, integer type) {
    list result = []; integer x = mixedList != [];
 
    if (type == TYPE_INTEGER) 
        while ((--x) >= 0) 
            result = [(integer)llList2String(mixedList, x)] + result;
    else if (type == TYPE_FLOAT) 
        while ((--x) >= 0) {
            if (llGetListEntryType(mixedList, x) == TYPE_FLOAT) result = [llList2Float(mixedList, x)] + result;
            else result = [(float)llList2String(mixedList, x)] + result;
        }
    else if (type == TYPE_VECTOR) 
        while ((--x) >= 0) {
            if (llGetListEntryType(mixedList, x) == TYPE_VECTOR) result = [llList2Vector(mixedList, x)] + result;
            else result = [(vector)llList2String(mixedList, x)] + result;
        }
    else if (type == TYPE_ROTATION) 
        while ((--x) >= 0) {
            if (llGetListEntryType(mixedList, x) == TYPE_ROTATION) result = [llList2Rot(mixedList, x)] + result;
            else result = [(rotation)llList2String(mixedList, x)] + result;
        }
    else if (type == TYPE_STRING) 
        while ((--x) >= 0) 
            result = [llList2String(mixedList, x)] + result;
    else if (type == TYPE_KEY) 
        while ((--x) >= 0) 
            result = [llList2Key(mixedList, x)] + result;
    else result = mixedList;
 
    return result;
}
Personal tools
In other languages