Difference between revisions of "LlGetListEntryType"

From Second Life Wiki
Jump to navigation Jump to search
m
m
 
(27 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{LSL_Function/negative index|true|index}}{{LSL_Function
{{LSL_Function
|inject-2={{LSL_Function/list/element|src|index|return={{LSL Const|TYPE_INVALID|integer|0|c=none}}}}
|func_id=194|func_sleep=0.0|func_energy=10.0
|func_id=194|func_sleep=0.0|func_energy=10.0
|func=llGetListEntryType|return_type=integer
|func=llGetListEntryType|return_type=integer|return_subtype=type
|p1_type=list|p1_name=src
|p1_type=list|p1_name=src
|p2_type=integer|p2_name=index
|p2_type=integer|p2_name=index
|func_footnote=If '''index''' is invalid then {{LSL Const|TYPE_INVALID|integer|0|c=none}} is returned.
|func_footnote=
|func_desc
|func_desc
|return_text=that is the type of the entry '''index''' in '''src'''.
|Return_text=of the entry at {{LSLP|index}} in {{LSLP|src}}.
|spec
|spec
|caveats
|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.
|constants=
|constants=
{{{!}}
{{LSL_Constants/ListEntryType}}
{{!}}- valign="top"
|examples=
{{!}}
<source lang="lsl2">
{{{!}}{{Prettytable}}
string get_list_entry_type_info(integer inputInteger)
{{!}}-{{Hl2}}
{
!colspan="2"{{!}}Type
    if (inputInteger == TYPE_INTEGER)
!Description
        return "integer";
{{!}}-
{{!}}{{LSL Const|TYPE_INTEGER|integer|1|c=integer}}
    else if (inputInteger == TYPE_FLOAT)
{{!}}{{#var:value}}
        return "float";
{{!}}[[integer]]
{{!}}-
    else if (inputInteger == TYPE_STRING)
{{!}}{{LSL Const|TYPE_FLOAT|integer|2|c=float}}
        return "string";
{{!}}{{#var:value}}
{{!}}[[float]]
    else if (inputInteger == TYPE_KEY)
{{!}}-
        return "key";
{{!}}{{LSL Const|TYPE_STRING|integer|3|c=string}}
{{!}}{{#var:value}}
    else if (inputInteger == TYPE_VECTOR)
{{!}}[[string]]
        return "vector";
{{!}}}
{{!}}
    else if (inputInteger == TYPE_ROTATION)
{{{!}}{{Prettytable}}
        return "rotation";
{{!}}-{{Hl2}}
!colspan="2"{{!}}Type
//  else
!Description
        return "<!-- invalid type --!>";
{{!}}-
}
{{!}}{{LSL Const|TYPE_KEY|integer|4|c=key}}
 
{{!}}{{#var:value}}
default
{{!}}[[key]]
{
{{!}}-
    touch_start(integer num_detected)
{{!}}{{LSL Const|TYPE_VECTOR|integer|5|c=vector}}
    {
{{!}}{{#var:value}}
        list listOfStuff = [
{{!}}[[vector]]
            <1.0, 2.0, 3.0, 4.0>,
{{!}}-
            <1.0, 2.0, 3.0>,
{{!}}{{LSL Const|TYPE_ROTATION|integer|6|c=rotation}}
            llGetKey(),
{{!}}{{#var:value}}
            "some random text",
{{!}}[[rotation]]
            382.4,
{{!}}}
            1];
{{!}}
 
{{{!}}{{Prettytable}}
        integer index = ~llGetListLength(listOfStuff);
{{!}}-{{Hl2}}
 
!colspan="2"{{!}}Type
        // start with -length and end with -1
!Description
        while (++index)
{{!}}-
        {
{{!}}{{LSL Const|TYPE_INVALID|integer|0|c=none}}
            integer type = llGetListEntryType(listOfStuff, index);
{{!}}{{#var:value}}
            string entry2string = llList2String(listOfStuff, index);
{{!}}{{#var:comment}}
 
{{!}}}
            llSay(0, "'" + entry2string + "' has the list-entry-type: '" + get_list_entry_type_info(type) + "'");
{{!}}}
        }
|examples
    }
|helpers
}
</source>
|helpers=This function has two primary uses: serializing a list to a string, debugging.
<source lang="lsl2">//A simple list serializer and deserializer.
//This will round floats to 6 decimal places and not handle strings with embedded "|" characters
 
string serialize(list input){
    integer len = ~llGetListLength(input);
    string build = (string)(~len);
    while(++len)
    {
        build += "|" + (string) llGetListEntryType(input, len) +
            "=" + llList2String(input, len);        //not safe if list item could contain "|" character
    }
    return build;
}
 
list deserialize(string input){
    integer len = ~(integer)input;//the length is everything before the "|", which is where (integer) stops
    list build = [];
    list pair = llParseString2List(input, ["|"], []);
    string value;
    list replace;
    while(++len)
    {
        integer type = (integer)(value = llList2String(pair, len));
        value = llDeleteSubString(value, 0, llSubStringIndex(value, "="));
        if (type == TYPE_INTEGER)
            replace = [(integer)value];
        else if (type == TYPE_FLOAT)
            replace = [(float)value];
        else if (type == TYPE_STRING )
            replace = [value];
        else if (type == TYPE_KEY)
            replace = [(key)value];
        else if (type == TYPE_VECTOR)
            replace = [(vector)value];
        else if (type == TYPE_ROTATION)
            replace = [(rotation)value];
        pair = llListReplaceList(pair, replace, len, len);
    }
    return pair;
}
</source>
This snippet produces an LSL formatted dump of a list that can be pasted back into LSL.
<source lang="lsl2">
//A list dumper that produces an output suitable for pasting back into LSL as a variable initialization.
string list_dump(list mylist)
{
    string output = "[";
    integer i;
    integer count = (mylist != [] );  // length of list (shorthand)
    integer type;
    for ( ; i < count; ++i)
    {
        if (i)
            output += ",";
        type = llGetListEntryType(mylist, i);
        if (type == TYPE_KEY)
            output += "(key)";
        if (type == TYPE_STRING || type == TYPE_KEY) 
            output += "\"";
        output += llList2String(mylist, i);
        if (type == TYPE_STRING || type == TYPE_KEY)
            output += "\"";
    }
    return (output + "]");
}
</source>
For a more complex and robust list (de)serializer see [[TightList|TightListType]].
|also_functions={{LSL DefineRow||[[llList2Float]]|}}
|also_functions={{LSL DefineRow||[[llList2Float]]|}}
{{LSL DefineRow||[[llList2Integer]]|}}
{{LSL DefineRow||[[llList2Integer]]|}}
Line 73: Line 144:
|permission
|permission
|cat1=List
|cat1=List
|cat2=List/Read Element
|cat2=List/Entry Types
|cat3
|cat3
|cat4
|cat4
}}
}}

Latest revision as of 19:32, 20 December 2015

Summary

Function: integer llGetListEntryType( list src, integer index );

Returns the type (an integer) of the entry at index in src.

• list src List containing the element of interest.
• integer index Index of the element of interest.

index supports negative indexes.
If index describes a location not in src then TYPE_INVALID is returned.

Specification

Index Positive Negative
First 0 -length
Last length - 1 -1

Indexes

  • Positive indexes count from the beginning, the first item being indexed as 0, the last as (length - 1).
  • Negative indexes count from the far end, the first item being indexed as -length, the last as -1.

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

Caveats

  • If index is out of bounds the script continues to execute without an error message.
  • 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.
All Issues ~ Search JIRA for related Bugs

Examples

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 "<!-- invalid type --!>";
}

default
{
    touch_start(integer num_detected)
    {
        list listOfStuff = [
            <1.0, 2.0, 3.0, 4.0>,
            <1.0, 2.0, 3.0>,
            llGetKey(),
            "some random text",
            382.4,
            1];

        integer index = ~llGetListLength(listOfStuff);

        // start with -length and end with -1
        while (++index)
        {
            integer type = llGetListEntryType(listOfStuff, index);
            string entry2string = llList2String(listOfStuff, index);

            llSay(0, "'" + entry2string + "' has the list-entry-type: '" + get_list_entry_type_info(type) + "'");
        }
    }
}

Useful Snippets

This function has two primary uses: serializing a list to a string, debugging.

//A simple list serializer and deserializer.
//This will round floats to 6 decimal places and not handle strings with embedded "|" characters

string serialize(list input){
    integer len = ~llGetListLength(input);
    string build = (string)(~len);
    while(++len)
    {
        build += "|" + (string) llGetListEntryType(input, len) + 
             "=" + llList2String(input, len);        //not safe if list item could contain "|" character
    }
    return build;
}

list deserialize(string input){
    integer len = ~(integer)input;//the length is everything before the "|", which is where (integer) stops
    list build = [];
    list pair = llParseString2List(input, ["|"], []);
    string value;
    list replace;
    while(++len)
    {
        integer type = (integer)(value = llList2String(pair, len));
        value = llDeleteSubString(value, 0, llSubStringIndex(value, "="));
        if (type == TYPE_INTEGER)
            replace = [(integer)value];
        else if (type == TYPE_FLOAT)
            replace = [(float)value];
        else if (type == TYPE_STRING )
            replace = [value];
        else if (type == TYPE_KEY)
            replace = [(key)value];
        else if (type == TYPE_VECTOR)
            replace = [(vector)value];
        else if (type == TYPE_ROTATION)
            replace = [(rotation)value];
        pair = llListReplaceList(pair, replace, len, len);
    }
    return pair;
}

This snippet produces an LSL formatted dump of a list that can be pasted back into LSL.

//A list dumper that produces an output suitable for pasting back into LSL as a variable initialization.
string list_dump(list mylist)
{
    string output = "[";
    integer i;
    integer count = (mylist != [] );   // length of list (shorthand)
    integer type;
    for ( ; i < count; ++i) 
    {
        if (i) 
            output += ","; 
        type = llGetListEntryType(mylist, i); 
        if (type == TYPE_KEY)
            output += "(key)";
        if (type == TYPE_STRING || type == TYPE_KEY)  
            output += "\""; 
        output += llList2String(mylist, i); 
        if (type == TYPE_STRING || type == TYPE_KEY)
            output += "\""; 
    }
    return (output + "]");
}

For a more complex and robust list (de)serializer see TightListType.

See Also

Functions

•  llList2Float
•  llList2Integer
•  llList2Key
•  llList2Rot
•  llList2String
•  llList2Vector

Articles

•  Negative Index

Deep Notes

Search JIRA for related Issues

Signature

function integer llGetListEntryType( list src, integer index );