Difference between revisions of "Separate Words"

From Second Life Wiki
Jump to navigation Jump to search
m
(those 0 to 16 lines of example trace were buggy -- these 0 to 33 lines shown in this edit are what I see when I paste all this code as is into Second Life fresh now)
Line 79: Line 79:
0: 42<br/>
0: 42<br/>
1: 0.99<br/>
1: 0.99<br/>
2: 00000000<br/>
2: "<br/>
3: 0000<br/>
3: 00000000<br/>
4: 0000<br/>
4: -<br/>
5: 0000<br/>
5: 0000<br/>
6: 000000000000<br/>
6: -<br/>
7: abc<br/>
7: 0000<br/>
8: def<br/>
8: -<br/>
9: xyz<br/>
9: 0000<br/>
10: zyx<br/>
10: -<br/>
11: 0<br/>
11: 000000000000<br/>
12: 1<br/>
12: "<br/>
13: 2<br/>
13: [<br/>
14: 3<br/>
14: abc<br/>
15: source<br/>
15: def<br/>
16: literals<br/>
16: ]<br/>
17: "<br/>
18: xyz<br/>
19: \<br/>
20: \<br/>
21: "<br/>
22: zyx<br/>
23: "<br/>
24: <<br/>
25: 0<br/>
26: 1<br/>
27: 2<br/>
28: 3<br/>
29: ><br/>
30: /<br/>
31: /<br/>
32: source<br/>
33: literals<br/>
OK
OK


Line 133: Line 150:


<div id="box">
<div id="box">
== See Also ==
== See Also ==
<div style="padding: 0.5em">
<div style="padding: 0.5em">

Revision as of 08:34, 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

Says:
0: 42
1: 0.99
2: "
3: 00000000
4: -
5: 0000
6: -
7: 0000
8: -
9: 0000
10: -
11: 000000000000
12: "
13: [
14: abc
15: def
16: ]
17: "
18: xyz
19: \
20: \
21: "
22: zyx
23: "
24: <
25: 0
26: 1
27: 2
28: 3
29: >
30: /
31: /
32: source
33: literals
OK

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