Difference between revisions of "LlGetListEntryType"

From Second Life Wiki
Jump to navigation Jump to search
m (added LSL Tip)
(Fix 2 non-compilable scripts. Fix 3rd script that broke many style rules. Supply missing 'key' type list entry.)
Line 6: Line 6:
|p2_type=integer|p2_name=index
|p2_type=integer|p2_name=index
|func_footnote=
|func_footnote=
{{LSL Tip|Please read [[User:Kireji_Haiku/How_to_deal_with_lists_in_LSL|this intro of how to iterate over a list in LSL]].}}
|func_desc
|func_desc
|return_text=that is the type of the entry {{LSLP|index}} in {{LSLP|src}}.
|return_text=that is the type of the entry {{LSLP|index}} in {{LSLP|src}}.
Line 47: Line 46:
             <1.0, 2.0, 3.0, 4.0>,
             <1.0, 2.0, 3.0, 4.0>,
             <1.0, 2.0, 3.0>,
             <1.0, 2.0, 3.0>,
             TEXTURE_BLANK,
             llGetKey(),
             "some random text",
             "some random text",
             382.4,
             382.4,
Line 60: Line 59:
             string entry2string = llList2String(listOfStuff, index);
             string entry2string = llList2String(listOfStuff, index);


            // PUBLIC_CHANNEL has the integer value 0
             llSay(0, "'" + entry2string + "' has the list-entry-type: '" + get_list_entry_type_info(type) + "'");
             llSay(PUBLIC_CHANNEL,
                "'" + entry2string + "' has the list-entry-type: '" + get_list_entry_type_info(type) + "'");
         }
         }
     }
     }
Line 74: Line 71:
     integer len = ~llGetListLength(input);
     integer len = ~llGetListLength(input);
     string build = (string)(~len);
     string build = (string)(~len);
     while(++len){
     while(++len)
         build += "|" + llGetListEntryType(input, len) +  
    {
                "=" + llList2String(input, len);//very unsafe, list item could contain "|" character!!!
         build += "|" + (string) llGetListEntryType(input, len) +  
            "=" + llList2String(input, len);       //not safe if list item could contain "|" character
     }
     }
     return build;
     return build;
Line 87: Line 85:
     string value;
     string value;
     list replace;
     list replace;
     while(++len){
     while(++len)
    {
         integer type = (integer)(value = llList2String(pair, len));
         integer type = (integer)(value = llList2String(pair, len));
         value = llDeleteSubString(value, 0, llSubStringIndex(value, "="))
         value = llDeleteSubString(value, 0, llSubStringIndex(value, "="));
         if(TYPE_INTEGER == type)
         if (TYPE_INTEGER == type)
             replace = [(integer)value];
             replace = [(integer)value];
         else if(TYPE_FLOAT == type)
         else if (TYPE_FLOAT == type)
             replace = [(float)value];
             replace = [(float)value];
         else if(TYPE_STRING == type)
         else if (type == TYPE_STRING )
             replace = [value];
             replace = [value];
         else if(TYPE_KEY == type)
         else if (type == TYPE_KEY)
             replace = [(key)value];
             replace = [(key)value];
         else if(TYPE_VECTOR == type)
         else if (type == TYPE_VECTOR)
             replace = [(vector)value];
             replace = [(vector)value];
         else if(TYPE_ROTATION == type)
         else if (type == TYPE_ROTATION)
             replace = [(rotation)value];
             replace = [(rotation)value];
         pair = llListReplaceList(pair, replace, len, len);
         pair = llListReplaceList(pair, replace, len, len);
Line 107: Line 106:
}
}
</lsl>
</lsl>
This snippet produces a LSL formatted dump of a list that can be pasted back into LSL.
This snippet produces an LSL formatted dump of a list that can be pasted back into LSL.
<lsl>
<lsl>
//A list dumper that produces an output suitable for pasting back into LSL as a variable initialization.
//A list dumper that produces an output suitable for pasting back into LSL as a variable initialization.
//Originally By Talarus Luan
string list_dump(list mylist)
string list_dump(list mylist)
{
{
     string s = "[";
     string output = "[";
     integer i;
     integer i;
     integer t;
     integer count = (mylist != [] );  // length of list (shorthand)
     for (i = 0; i < llGetListLength(mylist); ++i)  
    integer type;
     for ( ; i < count; ++i)  
     {
     {
         if (i > 0)  
         if (i)  
             s += ",";  
             output += ",";  
         t = llGetListEntryType(mylist,i);  
         type = llGetListEntryType(mylist, i);  
         if (t==4)
         if (type == TYPE_KEY)
             s+= "(key)";
             output += "(key)";
         if ((t == 3) || (t==4))   
         if (type == TYPE_STRING || type == TYPE_KEY)   
             s += "\""; s += llList2String(mylist,i);  
             output += "\"";  
         if ((t == 3) || (t==4))
        output += llList2String(mylist, i);  
             s += "\"";  
         if (type == TYPE_STRING || type == TYPE_KEY)
             output += "\"";  
     }
     }
     s += "]";
     return (output + "]");
    return s;
}
}
</lsl>
</lsl>

Revision as of 07:58, 4 January 2013

{{LSL_Function |inject-2= |func_id=194|func_sleep=0.0|func_energy=10.0 |func=llGetListEntryType|return_type=integer |p1_type=list|p1_name=src |p2_type=integer|p2_name=index |func_footnote= |func_desc |return_text=that is the type of the entry index in src. |spec |caveats=

  • If a vector is stored in a list as "<7,5,0>" (as a string type, as opposed to <7,5,0> which is a vector type), its type will be returned as TYPE_STRING, not TYPE_VECTOR. The same applies for "1" being returned as a string instead of an integer, etc. There is no easy way to guess what the type should be from a string value. The users intent may not be obvious

|constants=

Type Description
TYPE_INTEGER 1 integer
TYPE_FLOAT 2 float
TYPE_STRING 3 string
TYPE_KEY 4 key
TYPE_VECTOR 5 vector
TYPE_ROTATION 6 rotation
TYPE_INVALID 0 none

|examples= <lsl> string get_list_entry_type_info(integer inputInteger) {

   if (inputInteger == TYPE_INTEGER)
       return "integer";

   else if (inputInteger == TYPE_FLOAT)
       return "float";

   else if (inputInteger == TYPE_STRING)
       return "string";

   else if (inputInteger == TYPE_KEY)
       return "key";

   else if (inputInteger == TYPE_VECTOR)
       return "vector";

   else if (inputInteger == TYPE_ROTATION)
       return "rotation";

// else

       return "