Difference between revisions of "Separate Words"

From Second Life Wiki
Jump to navigation Jump to search
(work around the llParseString2List limit of 8 spacers or separators)
 
(Formatted according to LSL functions standard)
Line 1: Line 1:
{{LSL Header}}
{{LSL Header}}__NOTOC__
<div id="box">
{{#vardefine:p_src_desc|source string}}
{{#vardefine:p_separators_desc|separators to be discarded}}
{{#vardefine:p_spacers_desc|spacers to be kept}}
== Function: [[list]] SeparateWords([[string]] {{LSL Param|src}},[[string]] {{LSL Param|separators}},[[string]] {{LSL Param|spacers}}); ==
<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. Same as LSL funtion [[llParseString2List]], but not limited to 8 spacers or separators.


== Separate Words ==
Thus substitute a call to the [[llParseString2List]] function by a call to SeparateWords whenever you have more than 8 separators or more than 8 spacers.
 
LSL defines [[llParseString2List]] to divide a source string into words. That function sees the chars between separators or spacers, and each spacer, as a word (except that function never sees the empty string as a word).
 
Trouble is, LSL doesn't let you choose more than 8 spacers or separators, if you call llParseString2List directly.
 
You can substitute a call to the separateWords function here in place of your call to llParseString2List whenever you have more than 8 separators or more than 8 spacers.
 
The code follows:


{|
{{LSL DefineRow|[[string]]|src|{{#var:p_src_desc}}}}
{{LSL DefineRow|[[list]]|separators|{{#var:p_separators_desc}}}}
{{LSL DefineRow|[[list]]|spacers|{{#var:p_spacers_desc}}}}
|}
</div></div>
<div id="box">
== Specification ==
<div style="padding: 0.5em;">
<pre>
<pre>
// Call llParseString2List for each of the sources.
// Return the results in order.
// cf. http://wiki.secondlife.com/wiki/Separate_Words
list applyLlParseString2List(list sources, list separators, list spacers)
list applyLlParseString2List(list sources, list separators, list spacers)
{
{
     list words = [];
     list words = [];
    integer index;
     integer lenSources = llGetListLength(sources);
     integer lenSources = llGetListLength(sources);
     for (index = 0; index < lenSources; ++index)
    integer i = 0;
     for (; i < lenSources; ++i)
     {
     {
         string source = llList2String(sources, index);
         string source = llList2String(sources, i);
         words += llParseString2List(source, separators, spacers);
         words += llParseString2List(source, separators, spacers);
     }
     }
Line 29: Line 33:
}
}


// Divide a source string into words.
// Divide a source string into words
// See the chars between separators or spacers, and each spacer, as a word.
// See the chars between separators or spacers, and each spacer, as a word
// Never see the empty string as a word.
// Never see the empty string as a word
// cf. http://wiki.secondlife.com/wiki/Separate_Words


list separateWords(string chars, list separators, list spacers)
list SeparateWords(string src, list separators, list spacers)
{
{
      
      
     // Begin with all chars in one word
     // Begin with all chars in one word
      
      
     list words = [chars];
     list words = [src];
      
      
     // List the chars between spacers, and each spacer, as a word
     // List the chars between spacers, and each spacer, as a word
      
      
    integer index;
     integer lenSpacers = llGetListLength(spacers);
     integer lenSpacers = llGetListLength(spacers);
     for (index = 0; index < lenSpacers; index += 8)
    integer i = 0;
     for (; i < lenSpacers; i += 8)
     {
     {
         list some = llList2List(spacers, index, index + 8 - 1);
         list some = llList2List(spacers, i, i + 8 - 1);
         words = applyLlParseString2List(words, [], some);
         words = applyLlParseString2List(words, [], some);
     }         
     }         
Line 53: Line 56:
     // Discard the separators after letting the separators separate words
     // Discard the separators after letting the separators separate words
      
      
//  integer index;
     integer lenSeparators = llGetListLength(separators);
     integer lenSeparators = llGetListLength(separators);
     for (index = 0; index < lenSeparators; index += 8)
     for (i = 0; i < lenSeparators; i += 8)
     {
     {
         list some = llList2List(separators, index, index + 8 - 1);
         list some = llList2List(separators, i, i + 8 - 1);
         words = applyLlParseString2List(words, some, []);
         words = applyLlParseString2List(words, some, []);
     }
     }
Line 65: Line 67:
     return words;
     return words;
}
}
</pre>
</div></div>


// Demo
<div id="box">
 
== Example ==
<div style="padding: 0.5em;">
Example to separate this '''{{LSL Param|src}}''': <br/>
''42 0.99 \"00000000-0000-0000-0000-000000000000\" [abc, def] \"xyz\\\\\"zyx\" <0, 1, 2, 3> // source literals''
<pre>
string lf = "\n";
string lf = "\n";
string quote = "\"";
string quote = "\"";
Line 86: Line 94:
     {
     {
          
          
         string chars = "42 0.99 \"00000000-0000-0000-0000-000000000000\" [abc, def] \"xyz\\\\\"zyx\" <0, 1, 2, 3> // source literals";
         string chars = "42 0.99 \"00000000-0000-0000-0000-000000000000\"  
         list words = separateWords(chars, separators(), spacers);
        [abc, def] \"xyz\\\\\"zyx\" <0, 1, 2, 3> // source literals";
         list words = SeparateWords(chars, separators(), spacers);


        integer index;
         integer lenWords = llGetListLength(words);
         integer lenWords = llGetListLength(words);
         for (index = 0; index < lenWords; ++index)
        integer i = 0;
         for (; i < lenWords; ++i)
         {
         {
             llOwnerSay((string) index + ": " + llList2String(words, index));
             llOwnerSay((string) i + ": " + llList2String(words, i));
         }
         }
          
          
Line 100: Line 109:
}
}
</pre>
</pre>
</div></div>
<div id="box">
== See Also ==
<div style="padding: 0.5em">
'''Functions'''
* [[LlParseString2List|llParseString2List]]
</div></div>


[[Category:LSL Examples]]
{{LSLC|Examples|Separate Words}}

Revision as of 07:42, 12 September 2007


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

Returns a list that is src broken into a list, discarding separators, keeping spacers, discards any null values generated. Same as LSL funtion llParseString2List, but not limited to 8 spacers or separators.

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

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

Specification

list applyLlParseString2List(list sources, list separators, list spacers)
{
    list words = [];
    integer lenSources = llGetListLength(sources);
    integer i = 0;
    for (; i < lenSources; ++i)
    {
        string source = llList2String(sources, i);
        words += llParseString2List(source, separators, spacers);
    }
    return words;
}

// Divide a source string into words
// See the chars between separators or spacers, and each spacer, as a word
// Never see the empty string as a word

list SeparateWords(string src, list separators, list spacers)
{
    
    // Begin with all chars in one word
    
    list words = [src];
    
    // List the chars between spacers, and each spacer, as a word
     
    integer lenSpacers = llGetListLength(spacers);
    integer i = 0;
    for (; i < lenSpacers; i += 8)
    {
        list some = llList2List(spacers, i, i + 8 - 1);
        words = applyLlParseString2List(words, [], some);
    }        
    
    // Discard the separators after letting the separators separate words
     
    integer lenSeparators = llGetListLength(separators);
    for (i = 0; i < lenSeparators; i += 8)
    {
        list some = llList2List(separators, i, i + 8 - 1);
        words = applyLlParseString2List(words, some, []);
    }
    
    // Succeed
        
    return words;
}

Example

Example to separate this src:
42 0.99 \"00000000-0000-0000-0000-000000000000\" [abc, def] \"xyz\\\\\"zyx\" <0, 1, 2, 3> // source literals

string lf = "\n";
string quote = "\"";
string escape = "\\";

list spacers = [quote, "(", ")", "<", ">", "[", "]", "/", "+", "-", "*", "%", escape];

list separators()
{
    string tab = llUnescapeURL("%09"); // != "\t"
    string cr = llUnescapeURL("%0D"); // != "\r"
    return [tab, lf, cr, " ", ",", ";"];
}

default
{
    state_entry()
    {
        
        string chars = "42 0.99 \"00000000-0000-0000-0000-000000000000\" 
        	[abc, def] \"xyz\\\\\"zyx\" <0, 1, 2, 3> // source literals";
        list words = SeparateWords(chars, separators(), spacers);

        integer lenWords = llGetListLength(words);
        integer i = 0;
        for (; i < lenWords; ++i)
        {
            llOwnerSay((string) i + ": " + llList2String(words, i));
        }
        
        llOwnerSay("OK");
    }
}

See Also

Functions