Difference between revisions of "ListCast"

From Second Life Wiki
Jump to navigation Jump to search
(New page: {{LSL_Function |func=listCast |func_desc=Iterates through the contents of a list, producing a resulting list whose contents are cast to a single type. Please refer to [[LlGetListEntryType|...)
 
m (<lsl> tag to <source>)
 
(12 intermediate revisions by 3 users not shown)
Line 3: Line 3:
|func_desc=Iterates through the contents of a list, producing a resulting list whose contents are cast to a single type. Please refer to [[LlGetListEntryType|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).
|func_desc=Iterates through the contents of a list, producing a resulting list whose contents are cast to a single type. Please refer to [[LlGetListEntryType|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).
|return_type=list
|return_type=list
|return_text=A list whose contents are all one type.
|return_text=whose contents are all one type.
|p1_type=list|p1_name=mixedList|p1_desc=The list to cast, its contents may be anything.
|p1_type=list|p1_name=mixedList|p1_desc=The list to cast, its contents may be anything.
|p2_type=integer|p2_name=type|p2_desc=One of the TYPE_* variables, see [[LlGetListEntryType|llGetListEntryType()]] for more details.
|p2_type=integer|p2_name=type|p2_desc=The type of variable to cast all entries to.
|mode=user
|mode=user
|cat1=Examples
|cat1=Examples
|cat2=Library
|cat2=Library
|cat3=User-Defined Functions
|examples=<source lang="lsl2">
        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)));
</source>
}}
}}


= Implementation =
= Implementation =
<lsl>list listCast(list mixedList, integer type) {
<source lang="lsl2">list listCast(list mixedList, integer type) {
     list result = []; integer x = mixedList != [];
     list result = []; integer x = mixedList != [];


     if (type == TYPE_INTEGER)  
     if (type == TYPE_INTEGER)  
         while ((--x) >= 0)  
         while (x)  
             result = (result = []) + [llList2Integer(mixedList, x)] + result;
             result = [(integer)llList2String(mixedList, --x)] + result;
     else if (type == TYPE_FLOAT)  
     else if (type == TYPE_FLOAT)  
         while ((--x) >= 0)  
         while (x) {
            result = (result = []) + [llList2Float(mixedList, x)] + result;
            if (llGetListEntryType(mixedList, --x) == TYPE_FLOAT) result = [llList2Float(mixedList, x)] + result;
            else result = [(float)llList2String(mixedList, x)] + result;
        }
     else if (type == TYPE_VECTOR)  
     else if (type == TYPE_VECTOR)  
         while ((--x) >= 0)  
         while (x) {
            result = (result = []) + [(vector)llList2String(mixedList, x)] + result;
            if (llGetListEntryType(mixedList, --x) == TYPE_VECTOR) result = [llList2Vector(mixedList, x)] + result;
            else result = [(vector)llList2String(mixedList, x)] + result;
        }
     else if (type == TYPE_ROTATION)  
     else if (type == TYPE_ROTATION)  
         while ((--x) >= 0)  
         while (x) {
            result = (result = []) + [(rotation)llList2String(mixedList, x)] + result;
            if (llGetListEntryType(mixedList, --x) == TYPE_ROTATION) result = [llList2Rot(mixedList, x)] + result;
            else result = [(rotation)llList2String(mixedList, x)] + result;
        }
     else if (type == TYPE_STRING)  
     else if (type == TYPE_STRING)  
         while ((--x) >= 0)  
         while (x)  
             result = (result = []) + [llList2String(mixedList, x)] + result;
             result = [llList2String(mixedList, --x)] + result;
     else if (type == TYPE_KEY)  
     else if (type == TYPE_KEY)  
         while ((--x) >= 0)  
         while (x)  
             result = (result = []) + [llList2Key(mixedList, x)] + result;
             result = [llList2Key(mixedList, --x)] + result;
     else result = (mixedList = []) + mixedList;
     else result = mixedList;


     return (result = mixedList = []) + result;
     return result;
}</lsl>
}</source>


{{LSLC|Examples}}
{{LSLC|Examples}}

Latest revision as of 15:17, 22 January 2015

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)));

Implementation

list listCast(list mixedList, integer type) {
    list result = []; integer x = mixedList != [];

    if (type == TYPE_INTEGER) 
        while (x) 
            result = [(integer)llList2String(mixedList, --x)] + result;
    else if (type == TYPE_FLOAT) 
        while (x) {
            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) {
            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) {
            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) 
            result = [llList2String(mixedList, --x)] + result;
    else if (type == TYPE_KEY) 
        while (x) 
            result = [llList2Key(mixedList, --x)] + result;
    else result = mixedList;

    return result;
}