Difference between revisions of "List cast"
Jump to navigation
Jump to search
m (<lsl> tag to <source>) |
|||
(5 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
=== | {{LSL_Function | ||
< | |func=list_cast | ||
|func_desc=Iterates through the contents of a list, producing a resulting list whose contents are cast from strings to their identified type. This function is useful for converting a list of strings, such as from llParseString2List, to a list that could be used for llSetPrimitiveParams, or a list you know you are going to use a lot to save on casting all the time. | |||
|return_type=list | |||
|return_text=whose contents are cast to their identified types. | |||
|p1_type=list|p1_name=in|p1_desc=The list to cast, its contents should be all strings. | |||
|mode=user | |||
|cat1=Examples | |||
|cat2=Library | |||
|cat3=User-Defined Functions | |||
|caveats= | |||
* Any strings fed into this function (which are meant to stay strings) cannot contain pointed brackets (<>) They will be cast as vectors or rotations, if they do. | |||
|examples=<source lang="lsl2"> | |||
list in = ["1", "<1.0,2.5,0.5>", "0.8", "0", "Four", "8aef4875-6873-0919-f5ab-c9d6b48d18d4"]; | |||
in = list_cast(in); | |||
llSetScale( llList2Vector(in,1) ); | |||
llSetAlpha( llList2Float(in,2), llList2Integer(in,3) ); | |||
</source> | |||
}} | |||
= Implementation = | |||
<source lang="lsl2"> | |||
//This function typecasts a list of strings, into the types they appear to be. | //This function typecasts a list of strings, into the types they appear to be. | ||
//Extremely useful for feeding user data into llSetPrimitiveParams | //Extremely useful for feeding user data into llSetPrimitiveParams | ||
//It takes a list as an input, and returns that list, with all elements correctly typecast, as output | //It takes a list as an input, and returns that list, with all elements correctly typecast, as output | ||
//Written by Fractured Crystal, 27 Jan 2010, this | //Written by Fractured Crystal, 27 Jan 2010, Commissioned by WarKirby Magojiro, this function is Public Domain | ||
list list_cast(list in) | list list_cast(list in) | ||
{ | { | ||
Line 56: | Line 77: | ||
return out; | return out; | ||
} | } | ||
</ | </source> | ||
{{LSLC|Examples}} | |||
Latest revision as of 14:27, 22 January 2015
LSL Portal | Functions | Events | Types | Operators | Constants | Flow Control | Script Library | Categorized Library | Tutorials |
Summary
Function: list list_cast( list in );
Iterates through the contents of a list, producing a resulting list whose contents are cast from strings to their identified type. This function is useful for converting a list of strings, such as from llParseString2List, to a list that could be used for llSetPrimitiveParams, or a list you know you are going to use a lot to save on casting all the time.
Returns a list whose contents are cast to their identified types.
• list | in | – | The list to cast, its contents should be all strings. |
Caveats
- Any strings fed into this function (which are meant to stay strings) cannot contain pointed brackets (<>) They will be cast as vectors or rotations, if they do.
Examples
list in = ["1", "<1.0,2.5,0.5>", "0.8", "0", "Four", "8aef4875-6873-0919-f5ab-c9d6b48d18d4"];
in = list_cast(in);
llSetScale( llList2Vector(in,1) );
llSetAlpha( llList2Float(in,2), llList2Integer(in,3) );
Implementation
//This function typecasts a list of strings, into the types they appear to be.
//Extremely useful for feeding user data into llSetPrimitiveParams
//It takes a list as an input, and returns that list, with all elements correctly typecast, as output
//Written by Fractured Crystal, 27 Jan 2010, Commissioned by WarKirby Magojiro, this function is Public Domain
list list_cast(list in)
{
list out;
integer i = 0;
integer l= llGetListLength(in);
while(i < l)
{
string d= llStringTrim(llList2String(in,i),STRING_TRIM);
if(d == "")out += "";
else
{
if(llGetSubString(d,0,0) == "<")
{
if(llGetSubString(d,-1,-1) == ">")
{
list s = llParseString2List(d,[","],[]);
integer sl= llGetListLength(s);
if(sl == 3)
{
out += (vector)d;
//jump end;
}else if(sl == 4)
{
out += (rotation)d;
//jump end;
}
}
//either malformed,or identified
jump end;
}
if(llSubStringIndex(d,".") != -1)
{
out += (float)d;
}else
{
integer lold = (integer)d;
if((string)lold == d)out += lold;
else
{
key kold = (key)d;
if(kold)out += [kold];
else out += [d];
}
}
}
@end;
i += 1;
}
return out;
}