Scripting/Parsing

De DigiWiki.

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.

default
{
    touch_start(integer total_number)
    {
        string my_string = "Get/Rid/Of/Slashes/And/Put/In/Spaces";
        list my_parsed_string = llParseString2List(my_string, ["/"], []);
        my_string = llDumpList2String(my_parsed_string, " ");
        llOwnerSay(my_string);
    }
}
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,"><") + ">");
    }
}
// Basic gadget parsing
 
default
{
    state_entry()
    {
        llListen(PUBLIC_CHANNEL, "", llGetOwner(), "");
    }
 
    listen(integer channel, string name, key id, string message)
    {
        // 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
        // Assume message is " .s hug Sleepy"
        if (llList2String(command, 0) == ".s")
        {
            llOwnerSay("Action: " + llList2String(command, 1));
            llOwnerSay("Avatar: " + llList2String(command, 2));
        }
    }
}
// Basic searching
 
string FIND = "=p";
 
default
{
    state_entry()
    {
        llListen(PUBLIC_CHANNEL, "", 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;
        }
        llOwnerSay("WORD = " + WORD);
    }
}
default
{
    state_entry()
    {
        string csv = "first,second,third";
        list my_list = llCSV2List(csv);
        llOwnerSay("CSV: " + csv);
        integer num_list = llGetListLength(my_list);
        integer i;
        for (i = 0; i < num_list; ++i)
        {
            llOwnerSay("my_list[" + (string)i + "]: " + llList2String(my_list, i));
        }
    }
}
Outils personnels
  • Cette page a été consultée 1 555 fois.
donate
Google Ads