llParseString2List

From Second Life Wiki
Revision as of 04:15, 21 May 2014 by Cay Trudeau (talk | contribs)
Jump to navigation Jump to search

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 values generated.

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

Caveats

  • All empty strings (that would arise from a spacer or separator being adjacent to each other or the ends) are removed;
  • 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.
  • Only the first 8 separators and first 8 spacers from the beginning of the string will be used. Any beyond that will be ignored (see Useful Snippets section below)
  • You may however use several different separators, you just have to make a separate llParse2String2List call for each. That way only the length of string is your limit
All Issues ~ Search JIRA for related Bugs

Examples

You may use several different separators, you just have to make a separate llParse2String2List call for each. That way only the length of string is your limit. Do notice, that it usually means having more than 1 different separator the length of each item cannot be too many characters, so if you have to pass 16 keys, this is not the function for you

for example: string str = "item1::item2::item3::item4::item5::item6::item7::item8_1#item8_2#item8_3"; where item8 is divided into 3 more sub items separated by # <lsl> //string str = "item1::item2::item3::item4::item5::item6::item7::item8_1#item8_2#item8_3";

string shoppinglist = "macaroni::pepperoni::bread::sausage::coffee::syrup::apple::honey#raspberry#milk";

default {

   state_entry()
   {    
        list cmds = llParseString2List(shoppinglist, ["::"], []); // max 8 items / separator

       string item1 = llList2String(cmds, 0);  
       string item2 = llList2String(cmds, 1); 
       string item3 = llList2String(cmds, 2); 
       string item4 = llList2String(cmds, 3); 
       string item5 = llList2String(cmds, 4); 
       string item6 = llList2String(cmds, 5); 
       string item7 = llList2String(cmds, 6); 
       string item8 = llList2String(cmds, 7); 


         list cmds2 = llParseString2List(item8, ["#"], []);
       string item8_1 = llList2String(cmds2, 0);  
       string item8_2 = llList2String(cmds2, 1); 
       string item8_3 = llList2String(cmds2, 2);  

llOwnerSay("Last item on shopping list = " + item8_3");

   }

}


</lsl>


<lsl>


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

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.

<lsl>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?</lsl>

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 );