Recipe #2.3: String Parsing

    String parsing is the process whereby a string is divided into substrings so that the computer can understand the string. To see why string parsing is needed, consider a future example in this book. Later in this book recipes that allow a locking door to be installed will be demonstrated. This locking door allows the owner of the door to add other users to a list of allowed users.

    To add users to this door the owner must say their names. For example, to add the user “Encog Dod” the owner must say the following to the door.

door add Encog Dod

    To process this command properly, the door must divide the string into the words that make it up. This allows the door to make sure that the command starts with the word “door”. String parsing also allows the door to evaluate the command that was given to it.

    Listing 2.3 shows a simple script that parses strings.

Listing 2.3: String Parsing (StringParse.lsl)

// From the book:
//
// Scripting Recipes for Second Life
// by Jeff Heaton (Encog Dod in SL)
// ISBN: 160439000X
// Copyright 2007 by Heaton Research, Inc.
//
// This script may be freely copied and modified so long as this header
// remains unmodified.
//
// For more information about this book visit the following web site:
//
// http://www.heatonresearch.com/articles/series/22/



string text;

string pop()
{
    string result;
    integer i = llSubStringIndex(text, " ");
    
    if( i!=-1 )
    {
        i -=1;
        result = llGetSubString(text,0,i);
        text = llGetSubString(text,i+2,-1);
        return result;
    }
    else
    {
        result = text;
        text = "";
    }
    
    text = llStringTrim(text, STRING_TRIM);
    result = llStringTrim(result, STRING_TRIM);
    
    return result;
}



default
{
    state_entry()
    {
        text = "Now is the time for all good men to come to the aid of their country.";
        string str;
        
        while( (str=pop())!="" )
        {
            llSay(0,str);
        }
    }
}

    This recipe takes a string, such as “Testing one two three”, and then provides a method that breaks the string up by spaces. The previous string would become the following four strings: “Testing”, “one”, “two”, and “three”.

    To do this, the global variable text, must be set to the string to be parsed. Then, each time one element from text is needed, the pop function should be called. The pop function returns the first space delimited substring from the text variable and also removes what was returned from the text variable.

Implementing the pop Function

    The pop function accepts no parameters and returns a string. The returned string will be the next string parsed from the text variable.

string pop()
{

    This function begins by searching for the first occurrence of the space character.

    string result;
    integer i = llSubStringIndex(text, " ");

    If a space is found, extract the data from the text variable up to where the space was found. Also remove this string from the text variable.

    if( i!=-1 )
    {
        i -=1;
        result = llGetSubString(text,0,i);
        text = llGetSubString(text,i+2,-1);
        return result;
    }

    If no spaces are found, return the remaining characters in the text variable. The text variable is also set to an empty string, since there are no additional characters to parse.

    else
    {
        result = text;
        text = "";
    }

     Trim extra spaces from both the remaining text and the string just extracted.

    text = llStringTrim(text, STRING_TRIM);
    result = llStringTrim(result, STRING_TRIM);
    
    return result;
}

    Finally, the result is returned.

Testing String Parsing

    The script includes a simple state_entry function that tests the functions presented in this recipe. This demonstrates how the pop function behaves when passed a sentence.

default
{
    state_entry()
    {
        text = "Now is the time for all good men to come to the aid of their country.";
        string str;
        
        while( (str=pop())!="" )
        {
            llSay(0,str);
        }
    }
}

    The results from parsing the above sentence are:

[3:22]  Object: Now
[3:22]  Object: is
[3:22]  Object: the
[3:22]  Object: time
[3:22]  Object: for
[3:22]  Object: all
[3:22]  Object: good
[3:22]  Object: men
[3:22]  Object: to
[3:22]  Object: come
[3:22]  Object: to
[3:22]  Object: the
[3:22]  Object: aid
[3:22]  Object: of
[3:22]  Object: their
[3:22]  Object: country.

    This recipe shows how to break up a string by spaces. It could also be easily modified to parse a string in other ways. This is very similar to the built in function llParseString2List, except that the pop function does not require that every string be delimited by the space. At any point, the remaining text can be accessed by using the text value. For example “door add Encog Dod”, the parsing could stop after add and “Encog Dod” remains in the text string. If llParseString2List were used “Encog Dod” would have been split into two strings: “Encog” and “Dod”. If this functionality is not required, the llParseString2List function should be used.


Copyright 2005 - 2010 by Heaton Research, Inc.. Heaton Research™ and Encog™ are trademarks of Heaton Research. Click here for copyright and trademark information.