Difference between revisions of "LlParseString2List"

From Second Life Wiki
Jump to navigation Jump to search
m
Line 1: Line 1:
{{LSL_Function
{{LSL_Function
{{#vardefine:p_src_desc|source string}}
{{#vardefine:p_separators_desc|separators to be discarded}}
{{#vardefine:p_spacers_desc|spacers to be kept}}
{{#vardefine:p_ParseStringKeepNulls_desc|FALSE}}
|func_id=214|func_sleep=0.0|func_energy=10.0
|func_id=214|func_sleep=0.0|func_energy=10.0
|func=llParseString2List|return_type=list|p1_type=string|p1_name=src|p2_type=list|p2_name=separators|p3_type=list|p3_name=spacers
|func=llParseString2List|return_type=list
|p1_type=string|p1_name=src|p2_type=list|p2_name=separators|p3_type=list|p3_name=spacers
|func_footnote='''separators''' and '''spacers''' must be lists of strings, maximum of 8 each.
|func_footnote='''separators''' and '''spacers''' must be lists of strings, maximum of 8 each.
|func_desc
|func_desc
Line 29: Line 34:
}</pre>
}</pre>
|helpers=<div id="box">
|helpers=<div id="box">
{{#vardefine:p_src_desc|source string
== Function: [[list]] ParseString2List([[string]] {{LSL Param|src}},[[list]] {{LSL Param|separators}},[[list]] {{LSL Param|spacers}},[[integer]] {{LSL Param|ParseStringKeepNulls}}); ==
}}{{#vardefine:p_separators_desc|separators to be discarded
}}{{#vardefine:p_spacers_desc|spacers to be kept}}
{{#vardefine:p_KeepNulls_desc|FALSE}}
== Function: [[list]] ParseString2List([[string]] {{LSL Param|src}},[[list]] {{LSL Param|separators}},[[list]] {{LSL Param|spacers}},[[integer]] {{LSL Param|KeepNulls}}); ==
<div style="padding: 0.5em;">
<div style="padding: 0.5em;">
Returns a list that is '''{{LSL Param|src}}''' broken into a list, discarding '''{{LSL Param|separators}}''', keeping '''{{LSL Param|spacers}}''', discards any null values generated (set '''{{LSL Param|KeepNulls}}''' value [[FALSE]]).
Returns a list that is '''{{LSL Param|src}}''' broken into a list, discarding '''{{LSL Param|separators}}''', keeping '''{{LSL Param|spacers}}'''.
Same as '''[[llParseString2List]]''', but not limited to 8 spacers or separators.  
<div style="padding: 0.0em 0.5em 0.5em 0.5em;" id="box">
===if '''{{LSL Param|ParseStringKeepNulls}}''' == [[FALSE]]===
Same as '''[[llParseString2List]]''', but not limited to 8 spacers or separators.


Thus substitute a call to the '''[[llParseString2List]]''' function by a call to '''ParseString2List''' whenever you have more than 8 separators or more than 8 spacers.  
Thus substitute a call to the '''[[llParseString2List]]''' function by a call to '''ParseString2List''' whenever you have more than 8 separators or more than 8 spacers.  
</div><div style="padding: 0.0em 0.5em 0.5em 0.5em;" id="box">
===if '''{{LSL Param|ParseStringKeepNulls}}''' != [[FALSE]]===
Same as '''[[llParseStringKeepNulls]]''', but not limited to 8 spacers or separators.


Thus substitute a call to the '''[[llParseStringKeepNulls]]''' function by a call to '''ParseString2List''' whenever you have more than 8 separators or more than 8 spacers.
</div>
<pre>
<pre>
list ParseString2List(string src, list separators, list spacers, integer ParseStringKeepNulls)
list ParseString2List(string src, list separators, list spacers, integer ParseStringKeepNulls)

Revision as of 23:38, 17 September 2007

Summary

Function: list llParseString2List( string src, list separators, list spacers );
0.0 Forced Delay
10.0 Energy

Returns a list that is src broken into a list, discarding separators, keeping spacers, discards any null values generated.

• string src source string
• list separators separators to be discarded
• list spacers spacers to be kept

separators and spacers must be lists of strings, maximum of 8 each.

Caveats

  • To avoid contradiction, every string of the spacers list of strings to keep must not exist in the separators list of strings to discard.
  • Every element in the list will be a string, no matter what you think it should be. Cast the results of llList2String to get what you want.
    • integer my_int = (integer)llList2String(my_list, i);
  • llList2String is used here, as llList2Integer can only handle integers in simple notation, ie. will not handle hexadecimal integers.

Examples

default
{
    state_entry()
    {
        // This will say:
        // <A><crazy><fox><.><Saw><the><moon><.><.>
        string my_string = "A crazy fox.  Saw the moon..";
        list my_list = llParseString2List(my_string,[" "],["."]);
        llOwnerSay("<" + llDumpList2String(my_list,"><") + ">");
        
        // This will say:
        //  <A><crazy><fox><.><><><Saw><the><moon><.><><.><>
        my_list = llParseStringKeepNulls(my_string,[" "],["."]);
        llOwnerSay("<" + llDumpList2String(my_list,"><") + ">");
    }
}

Useful Snippets

Function: list ParseString2List(string src,list separators,list spacers,integer ParseStringKeepNulls);

Returns a list that is src broken into a list, discarding separators, keeping spacers.

if ParseStringKeepNulls == FALSE

Same as llParseString2List, but not limited to 8 spacers or separators.

Thus substitute a call to the llParseString2List function by a call to ParseString2List whenever you have more than 8 separators or more than 8 spacers.

if ParseStringKeepNulls != FALSE

Same as llParseStringKeepNulls, but not limited to 8 spacers or separators.

Thus substitute a call to the llParseStringKeepNulls function by a call to ParseString2List whenever you have more than 8 separators or more than 8 spacers.

list ParseString2List(string src, list separators, list spacers, integer ParseStringKeepNulls)
{//works just like llParseString2List and llParseStringKeepNulls but without 
 //the limits on spacers or separators
    list keys;
    integer i = spacers != [];
    integer offset;
    integer test;
    list out;
    string p;
    while(i)
        if((p = llList2String(spacers, i = ~-i)))
        {
            if(~(offset = llListFindList(out, (list)p)))
                keys = llListReplaceList(keys, (list)(~i), offset = -~(offset << 1), offset);
            else if(~(test = llSubStringIndex(src, p)))
            {
                keys = test + ((~i) + keys);
                out = p + out;
            }
        }
    for(i = separators != [];i;)
        if((p = llList2String(separators, i = ~-i)))
        {
            if(~(offset = llListFindList(out, (list)p)))
                keys = llListReplaceList(keys, (list)i, offset = -~(offset << 1), offset);
            else if(~(test = llSubStringIndex(src, p)))
            {
                keys = test + (i + keys);
                out = p + out;
            }
        }
    out = [];
    offset = 0;
    while(keys)
    {
        list t = llList2List(llListSort(keys, 2, TRUE), 0, 1);
        integer r = llListFindList(keys, t);
        if((i = llList2Integer(t, 0)) < offset)
            keys = llDeleteSubList(keys, r, -~r);
        else
        {
            if(offset != i || ParseStringKeepNulls)
                out += llDeleteSubString(src, i - offset, -1);
            if(0x80000000 & test = llList2Integer(t, 1))
                out += p = llList2String(spacers, ~test);
            else
                p = llList2String(separators, test);
            src = llDeleteSubString(src, 0, ~(offset - (i += llStringLength(p))));
            offset = i;
            if(~(test = llSubStringIndex(src, p)))
                keys = llListReplaceList(keys, (list)(test + offset), r, r);
            else
                keys = llDeleteSubList(keys, r, -~r);
        }
    }
    if(src != "" || ParseStringKeepNulls)
        return out + src;
    return out;
}//Strife Onizuka

Deep Notes

Signature

function list llParseString2List( string src, list separators, list spacers );