Difference between revisions of "User:Fred Gandt/Scripts/Functions"

From Second Life Wiki
Jump to navigation Jump to search
m (Right now I'm just thinking out loud. I have no idea how this will end up.)
Line 65: Line 65:
}</lsl>
}</lsl>


=== MultiStringReplace ( V1 ) ===
=== MultiStringReplace ( V2 ) ===


'''Replace parts of a string with other strings.'''
'''Replace parts of a string with other strings.'''


<lsl>// V1 //
<lsl>// V2 //


string MultiStringReplace(string src, list thisnthats)
string MultiStringReplace(string src, list thisnthats)
Line 79: Line 79:
     {
     {
         string this = llList2String(thisnthats, lc);
         string this = llList2String(thisnthats, lc);
        string that = llList2String(thisnthats, (++lc));
        integer sl = (llStringLength(this) - 1);
         while((index = llSubStringIndex(src, this)) != -1)
         while((index = llSubStringIndex(src, this)) != -1)
         src = llInsertString(llDeleteSubString(src, index, (index + sl)), index, that);
         src = llInsertString(llDeleteSubString(src, index, (index + (llStringLength(this) - 1))),
                            index, llList2String(thisnthats, (lc + 1)));
     }
     }
     while((++lc) < ll);
     while((lc += 2) < ll);
     return src;
     return src;
}
string source = "How much wood would a woodchuck chuck if a woodchuck could chuck wood?";
default
{
    state_entry()
    {
        llOwnerSay(MultiStringReplace(source, ["woodchuck", "primrezzer", "much", "many", "wood", "prims", "chuck", "rez"]));
        // Return == "Object: How many prims would a primrezzer rez if a primrezzer could rez prims?"
       
        llOwnerSay(MultiStringReplace(source, ["much", "many", "wood", "prims", "woodchuck", "primrezzer", "chuck", "rez"]));
        // Return == "Object: How many prims would a primsrez rez if a primsrez could rez prims?"
       
        llOwnerSay(MultiStringReplace(source, ["farmers", "primrezzer", "much", "many", "wood", "prims", "chuck", "rez"]));
        // Return == "Object: How many prims would a primsrez rez if a primsrez could rez prims?"
       
        llOwnerSay(MultiStringReplace(source, ["much", "many", "wood", "prims", "farmers", "primrezzer", "chuck", "rez"]));
        // Return == "Object: How many prims would a primsrez rez if a primsrez could rez prims?"
       
        llOwnerSay(MultiStringReplace(source, ["woodchuck", "primrezzer", "much", "many", "wood", "prims", "farmers", "primrezzer", "chuck", "rez"]));
        // Return == "Object: How many prims would a primrezzer rez if a primrezzer could rez prims?"
    }
}</lsl>
}</lsl>

Revision as of 20:21, 2 May 2010

FG jpg.jpg

My Contributions

I have implemented a V# system to make it more obvious if a function is updated. The V# forms part of the title of each function.

If you have any comments about the content of this page please post them HERE

All my scripting is written for compilation as MONO

More Pages

Free Scripts (content constantly updating)

More Free Scripts (content constantly updating)

Even More Free Scripts (content constantly updating)

Even More More Free Scripts (content constantly updating)

Even More More More Free Scripts (content constantly updating)

Functions for specific tasks (this page)

Legal Stuff

The legal stuff about contributing to this wiki (worth reading).

PJIRA Issue Tracker

The issues I have filed on the PJIRA

Tuition

Tuition scripts, notes, videos and screenshots etc. (hardly any content yet)

Functions

Here is some stuffs wot does fings

GetUniqueListEntries ( V1 )

Feed a list in and get out a list which contains no duplicate entries. Nice and fast.

<lsl>// V1 //

list GetUniqueListEntries(list src) {

   integer index = 0;
   list output = [];
   list entry = [];
   do
   {
       output += (entry = llList2List(src, 0, 0));
       src = llDeleteSubList(src, 0, 0);
       while((index = llListFindList(src, entry)) != -1)
       src = llDeleteSubList(src, index, index);
   }
   while(llGetListLength(src));
   return output;

}</lsl>

MultiStringReplace ( V2 )

Replace parts of a string with other strings.

<lsl>// V2 //

string MultiStringReplace(string src, list thisnthats) {

   integer index = 0;
   integer lc = 0;
   integer ll = llGetListLength(thisnthats);
   do
   {
       string this = llList2String(thisnthats, lc);
       while((index = llSubStringIndex(src, this)) != -1)
       src = llInsertString(llDeleteSubString(src, index, (index + (llStringLength(this) - 1))),
                            index, llList2String(thisnthats, (lc + 1)));
   }
   while((lc += 2) < ll);
   return src;

}

string source = "How much wood would a woodchuck chuck if a woodchuck could chuck wood?";

default {

   state_entry()
   {
       llOwnerSay(MultiStringReplace(source, ["woodchuck", "primrezzer", "much", "many", "wood", "prims", "chuck", "rez"]));
       // Return == "Object: How many prims would a primrezzer rez if a primrezzer could rez prims?"
       
       llOwnerSay(MultiStringReplace(source, ["much", "many", "wood", "prims", "woodchuck", "primrezzer", "chuck", "rez"]));
       // Return == "Object: How many prims would a primsrez rez if a primsrez could rez prims?"
       
       llOwnerSay(MultiStringReplace(source, ["farmers", "primrezzer", "much", "many", "wood", "prims", "chuck", "rez"]));
       // Return == "Object: How many prims would a primsrez rez if a primsrez could rez prims?"
       
       llOwnerSay(MultiStringReplace(source, ["much", "many", "wood", "prims", "farmers", "primrezzer", "chuck", "rez"]));
       // Return == "Object: How many prims would a primsrez rez if a primsrez could rez prims?"
       
       llOwnerSay(MultiStringReplace(source, ["woodchuck", "primrezzer", "much", "many", "wood", "prims", "farmers", "primrezzer", "chuck", "rez"]));
       // Return == "Object: How many prims would a primrezzer rez if a primrezzer could rez prims?"
   }

}</lsl>