Difference between revisions of "LlParseString2List"

From Second Life Wiki
Jump to navigation Jump to search
m (How very odd, this must have been an early article when I was designing the layout.)
m (<lsl> tag to <source>)
 
(19 intermediate revisions by 4 users not shown)
Line 5: Line 5:
|p2_type=list|p2_name=separators|p2_desc=separators to be discarded
|p2_type=list|p2_name=separators|p2_desc=separators to be discarded
|p3_type=list|p3_name=spacers|p3_desc=spacers to be kept
|p3_type=list|p3_name=spacers|p3_desc=spacers to be kept
|func_footnote
|func_footnote=In most situations [[llParseStringKeepNulls]] should be used instead. Discarding null values is rarely desired.
|func_desc
|func_desc
|return_text=that is {{LSLP|src}} broken into a list of strings, discarding {{LSLP|separators}}, keeping {{LSLP|spacers}}, discards any null values generated.
|return_text=that is {{LSLP|src}} broken into a list of strings, discarding {{LSLP|separators}}, keeping {{LSLP|spacers}}, discards any null (empty string) values generated.
|spec
|spec
|caveats=
|caveats=
*All empty strings (that would arise from a spacer or separator being adjacent to each other or the ends) are removed;
*All empty strings (that would arise from a spacer or separator being adjacent to each other or the ends) are removed;
**If you want them (to keep the order of a list, for example) use [[llParseStringKeepNulls]] instead;
**If you want them (to keep the order of a list, for example) use [[llParseStringKeepNulls]] instead;
* Only the first 8 separators and first 8 spacers that you specify will be used. Any beyond that will be ignored (see Useful Snippets section below if this is an issue for you);
*Only the first 8 separators and first 8 spacers supplied will be used. Any beyond that will be ignored. To work around this see [[#Useful Snippets]] section below. The only limit on the number of items in the output is available script memory.
* All separators and spacers must be strings. All other types will be ignored;
* All separators and spacers must be strings. All other types will be ignored;
* Separators take precedent over spacers. The string is parsed from start to finish. Each position is compared against the separators then spacers before moving onto the next position;
* Separators take precedent over spacers. The string is parsed from start to finish. Each position is compared against the separators then spacers before moving onto the next position;
Line 18: Line 18:
* All elements in the list returned by llParseString2List are strings, and must be explicitly typecast if they are to be used as other types. Do not rely upon the implicit typecasting of the other llList2* functions (as they typically return a default value);
* All elements in the list returned by llParseString2List are strings, and must be explicitly typecast if they are to be used as other types. Do not rely upon the implicit typecasting of the other llList2* functions (as they typically return a default value);
* Remember to capture the result of the operation with a variable, unless you are planning to act directly on the results.
* Remember to capture the result of the operation with a variable, unless you are planning to act directly on the results.
|constants
|examples=
|examples=<lsl>default
<source lang="lsl2">default
{
{
     state_entry()
     state_entry()
Line 34: Line 34:
         llOwnerSay("<" + llDumpList2String(my_list,"><") + ">");
         llOwnerSay("<" + llDumpList2String(my_list,"><") + ">");
     }
     }
}</lsl>
}</source>
 
While LSL does not support lists-in-lists, you can emulate lists-in-lists by making successive calls or you could use {{LSLGC|JSON}}.
 
In this example some items have additional information supplied.
<source lang="lsl2"> 
string shoppinglist = "macaroni::pepperoni::bread#wheat::sausage#italian::coffee::syrup::apple::ice cream#strawberry#chocolate#vanilla";
 
default
{
    state_entry()
    {
        list items = llParseString2List(shoppinglist, ["::"], []);
        integer i = 0;
        integer j = llGetListLength(items);
        for(;i < j; ++i)
        {
            list desc = llParseString2List(llList2String(items, i), ["#"], []);
            if(llGetListLength(desc) > 1)
            {
                list types = llDeleteSubList(desc,0,0);
                llOwnerSay("Item: "+ llList2String(desc, 0) + "  Type: " + llList2CSV(llDeleteSubList(types,-2,-1) + llDumpList2String(llList2List(types,-2,-1), " & ")));
            } else {
                llOwnerSay("Item: "+ (string)desc);
            }
        }
    }
}</source>
|helpers=
|helpers=
'''Examples of processing more than 8 spacers or separators:'''
'''Examples of processing more than 8 spacers or separators:'''
Line 50: Line 77:
|also_articles={{LSL DefineRow||[[Separate Words]]}}
|also_articles={{LSL DefineRow||[[Separate Words]]}}
{{LSL DefineRow||[[LSL-Editor/Bugs]]}}
{{LSL DefineRow||[[LSL-Editor/Bugs]]}}
|notes=If you indicate that something is a separator, it will split the string where it finds the indicated separator(s), and strip out the separator.
|notes=If you indicate some items as separators, it will split the string where it finds the indicated separators, and strip out the separators.


If instead you indicate that something is a spacer, it will split the string where it finds the spacer(s), but leave the spacer there and include it in the results.
If instead you indicate some items as spacers, it will split the string where it finds the spacers, but leave the spacers there, including them as separate entries in the result list.


<lsl>string myString = "What Are You Looking At?";
<source lang="lsl2">string myString = "What Are You Looking At?";


llSay(0, (string)llParseString2List(myString, ["W", "A", "Y", "L", "A"], [] ));
llSay(0, llList2CSV( llParseString2List(myString, ["W", "A", "Y", "L"], [] ) ) );
//returns: hat re ou ooking t?
//returns: hat , re , ou , ooking , t?
      
      
llSay(0, (string)llParseString2List(myString, [], ["W", "A", "Y", "L", "A"] ) );
llSay(0, llList2CSV( llParseString2List(myString, [], ["W", "A", "Y", "L"] ) ) );
//returns: What Are You Looking At?</lsl>
//returns: W, hat , A, re , Y, ou , L, ooking , A, t?</source>


Using " " as a separator will parse a sentence into words.
Using " " as a separator will parse a sentence into words.

Latest revision as of 14:08, 22 January 2015

Summary

Function: list llParseString2List( string src, list separators, list spacers );

Returns a list that is src broken into a list of strings, discarding separators, keeping spacers, discards any null (empty string) values generated.

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

In most situations llParseStringKeepNulls should be used instead. Discarding null values is rarely desired.

Caveats

  • All empty strings (that would arise from a spacer or separator being adjacent to each other or the ends) are removed;
  • Only the first 8 separators and first 8 spacers supplied will be used. Any beyond that will be ignored. To work around this see #Useful Snippets section below. The only limit on the number of items in the output is available script memory.
  • All separators and spacers must be strings. All other types will be ignored;
  • Separators take precedent over spacers. The string is parsed from start to finish. Each position is compared against the separators then spacers before moving onto the next position;
  • Duplicate separators and spacers have no ill effects;
  • All elements in the list returned by llParseString2List are strings, and must be explicitly typecast if they are to be used as other types. Do not rely upon the implicit typecasting of the other llList2* functions (as they typically return a default value);
  • Remember to capture the result of the operation with a variable, unless you are planning to act directly on the results.
All Issues ~ Search JIRA for related Bugs

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,"><") + ">");
    }
}

While LSL does not support lists-in-lists, you can emulate lists-in-lists by making successive calls or you could use JSON.

In this example some items have additional information supplied.

   
string shoppinglist = "macaroni::pepperoni::bread#wheat::sausage#italian::coffee::syrup::apple::ice cream#strawberry#chocolate#vanilla";

default
{
    state_entry()
    {
        list items = llParseString2List(shoppinglist, ["::"], []);
        integer i = 0;
        integer j = llGetListLength(items);
        for(;i < j; ++i)
        {
            list desc = llParseString2List(llList2String(items, i), ["#"], []);
            if(llGetListLength(desc) > 1)
            {
                list types = llDeleteSubList(desc,0,0);
                llOwnerSay("Item: "+ llList2String(desc, 0) + "  Type: " + llList2CSV(llDeleteSubList(types,-2,-1) + llDumpList2String(llList2List(types,-2,-1), " & ")));
            } else {
                llOwnerSay("Item: "+ (string)desc);
            }
        }
    }
}

Useful Snippets

Examples of processing more than 8 spacers or separators:

•  ParseString2List Functions exactly the same as llParseString2List and llParseStringKeepNulls.
•  separateWords Functions exactly the same as llParseString2List unless you violate it's additional preconditions.

Appears to be correct at a glance.

Notes

If you indicate some items as separators, it will split the string where it finds the indicated separators, and strip out the separators.

If instead you indicate some items as spacers, it will split the string where it finds the spacers, but leave the spacers there, including them as separate entries in the result list.

string myString = "What Are You Looking At?";

llSay(0, llList2CSV( llParseString2List(myString,  ["W", "A", "Y", "L"], [] ) ) );
//returns:  hat , re , ou , ooking , t?
    
llSay(0, llList2CSV( llParseString2List(myString, [], ["W", "A", "Y", "L"] ) ) );
//returns: W, hat , A, re , Y, ou , L, ooking , A, t?

Using " " as a separator will parse a sentence into words.

If there is no spacer you care about, just use [] as the spacer.

If an empty string is used as a separator or a spacer, it will have no effect.

See Also

Functions

•  llParseStringKeepNulls
•  llDumpList2String
•  llCSV2List
•  llList2CSV

Articles

•  Separate Words
•  LSL-Editor/Bugs

Deep Notes

History

Search JIRA for related Issues

Footnotes

  1. ^ Early release notes were not very accurate or thorough, they sometimes included information about features added in previous releases or failed to include information about features added in that release.

Signature

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