Don't click here unless you want to be banned.

LSL Wiki : parsing

HomePage :: PageIndex :: RecentChanges :: RecentlyCommented :: UserSettings :: You are ia360925.us.archive.org

Parsing


Parsing is analyzing a set of characters, which is known as a string, and (possibly) breaking them into sections. Parsing is useful when grabbing information from a text holder, like a file (notecard), inworld chat, or scripting event. These examples below show some common uses of parsing.



Examples of parsing:



string gParse = "Get/Rid/Of/Slashes/And/Put/In/Spaces";

default
{
    touch_start(integer total_number)
    {
        // Parse the string by taking out the slashes while turning the characters in between the slashes to list elements
        list parsedString = llParseString2List(gParse, ["/"], []);

        // Turn the list into a string, and stick a space between each element
        gParse = llDumpList2String(parsedString, [" "]);

        llOwnerSay(gParse);
    }
}



// Basic gadget parsing

default
{
    state_entry()
    {
        llListen(0, "", llGetOwner(), "");
    }
    
    listen(integer channel, string name, key id, string message)
    {
        // Assume message is " .s hug Sleepy"
        
        // Clear any extra spaces at the begining or end of message
        message = llStringTrim(message, STRING_TRIM);
        
        // Break up the message at spaces
        list command = llParseString2List(message, [" "], []);
        
        // If the string is a command, display gathered data
        if(llList2String(command, 0) == ".s") {
            llOwnerSay("Action: " + llList2String(command, 1));
            llOwnerSay("Avatar: " + llList2String(command, 2));
        }
    }
}


// Basic searching

string FIND = "=p";

default
{
    state_entry()
    {
        llListen(0, "", llGetOwner(), "");
    }
    
    listen(integer channel, string name, key id, string message)
    {
        // Trim all messages, turn them upper and lower case,
        // and decide if the string "=p" is in the message
        
        llOwnerSay("Lower case message: " + llToLower(message));
        llOwnerSay("Upper case message: " + llToUpper(message));
        
        // String search:
        // ~ is the same as not -1
        // Also can be written as:
        // if(llSubStringIndex(message, FIND) != -1)
        if(~llSubStringIndex(message, FIND))
            llOwnerSay(FIND + " was found in the string search");
        
        // A way to do this with a list is:
        list search = llParseString2List(message, [" "], []);
        
        if(~llListFindList(search, [FIND]))
            llOwnerSay(FIND + " was found in the list search");
    }
    
}


// This script shows how to add spaces in front, in back, or in between each letter of a string

integer NUMBER_OF_SPACES = 10;
integer FRONT            = 1;  // -1 is in back, 0 is in front,
                               // and 1 is inbetween each letter

string  WORD             = "Abracadabra";

default
{
    state_entry()
    {
        if(FRONT) // 1
            while(NUMBER_OF_SPACES)
                WORD = llInsertString(WORD, NUMBER_OF_SPACES--, " ");
        else if(~FRONT) // 0
            while(NUMBER_OF_SPACES--)
                WORD += " ";
        else // -1
            while(NUMBER_OF_SPACES--)
                WORD = " " + WORD;
    }
}


There is no comment on this page. [Display comments/form]