User:Fred Gandt/Scripts/Functions

From Second Life Wiki
< User:Fred Gandt‎ | Scripts
Revision as of 23:25, 7 May 2010 by Fred Gandt (talk | contribs) (Fixed bug.)
Jump to navigation Jump to search
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;

}

// Usage Example //

list source = ["How", "much", "wood", "would", "a", "woodchuck", "chuck", "if", "a", "woodchuck", "could", "chuck", "wood", "?"];

default {

   state_entry()
   {
       llOwnerSay(llDumpList2String(GetUniqueListEntries(source), ", "));
       // Return == "Object: How, much, wood, would, a, woodchuck, chuck, if, could, ?"
   }

}</lsl>

MultiListReplace ( V1 )

Replace entries in a list with other entries.

<lsl>// V1 //

list MultiListReplace(list src, list thisnthats) {

   integer index = 0;
   integer lc = 0;
   integer ll = llGetListLength(thisnthats);
   do
   {
       list this = llList2List(thisnthats, lc, lc);
       ++lc;
       while((index = llListFindList(src, this)) != -1)
       src = llListReplaceList(src, llList2List(thisnthats, lc, lc), index, index);
   }
   while((++lc) < ll);
   return src;

}

// Usage Example //

list source = ["How", "much", "wood", "would", "a", "woodchuck", "chuck", "if", "a", "woodchuck", "could", "chuck", "wood", "?"];

default {

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

}</lsl>

MultiStringReplace ( V4 )

Replace parts of a string with other strings.

<lsl>// V4 //

string MultiStringReplace(string src, list thisnthats) {

   integer index = 0;
   integer lc = -1;
   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));
   }
   while((++lc) < ll);
   return src;

}

// Usage Examples //

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", "piggies", "chuck", "rez"]));
       // Return == "Object: How many prims would a primrezzer rez if a primrezzer could rez prims?"
   }

}</lsl>

StrideOfList ( V2 )

Get strided entries from a list. And yes, I know about llList2ListStrided; Frankly, this works better.

<lsl>// V2 //

list StrideOfList(list src, integer stride, integer start, integer end) {

   list l = [];
   integer ll = llGetListLength(src);
   if(start < 0)start += ll;
   if(end < 0)end += ll;
   if(end < start) return llList2List(src, start, start);
   while(start <= end)
   {
       l += llList2List(src, start, start);
       start += stride;
   }
   return l;

}

// Usage Examples //

list source = ["How", "much", "wood", "would", "a", "woodchuck", "chuck", "if", "a", "woodchuck", "could", "chuck", "wood?"];

default {

   state_entry()
   {
       llOwnerSay(llDumpList2String(StrideOfList(source, 1, 0, -1), ", "));
       // Return == "Object: How, much, wood, would, a, woodchuck, chuck, if, a, woodchuck, could, chuck, wood?"

       llOwnerSay(llDumpList2String(StrideOfList(source, 2, 0, -2), ", "));
       // Return == "Object: How, wood, a, chuck, a, could"

       llOwnerSay(llDumpList2String(StrideOfList(source, 4, 3, -1), ", "));
       // Return == "Object: would, if, chuck"

       llOwnerSay(llDumpList2String(StrideOfList(source, 5, 3, -4), ", "));
       // Return == "Object: would, a"

       llOwnerSay(llDumpList2String(StrideOfList(source, 3, -10, -7), ", "));
       // Return == "Object: would, chuck"

       llOwnerSay(llDumpList2String(StrideOfList(source, 3, 7, -7), ", ")); // Start index is closer to end that end index.
       // Return == "Object: if"                                            // Start is returned only.
   }

}</lsl>

ToNormal ( V2 )

Returns a string that holds true to a few simple rules.

  • First person reference "I" is capitalized.
  • Specified names are capitalized.
  • The first word of each proper sentence is capitalized.

I would imagine the results will be a mess if the source is not neatly written. I'll work on improving it as time goes on (it's a start).

  • New-lines disturb the results. The return is not as desired. It's on the "to-do" list.

<lsl>// V2 //

string ToNormal(string src, list names) {

   list abc = ["a","b","c","d","e","f","g","h","i","j","k","l","m",
               "n","o","p","q","r","s","t","u","v","w","x","y","z"];
   list punc = [". ","; ","? ","! "];
   src = llToLower(" " + src + " ");
   string result = "";
   integer index = 0;
   integer lc = 0;
   integer ll = 0;
   integer fp = 1;
   string ko = "";
   if((ll = llGetListLength(names)))
   {
       do
       {
           string name = llToLower(llList2String(names, lc));
           while((index = llSubStringIndex(src, name)) != -1)
           src = llInsertString(llDeleteSubString(src, index, (index + (llStringLength(name) - 1))),
                                index, llList2String(names, lc));
       }
       while((++lc) < ll);
       index = 0;
   }
   while((index = llSubStringIndex(src, "i")) != -1)
   {
       if(fp)
       {
           ko = llGetSubString(src, 0, (index - 1));
           fp = 0;
       }
       if((llListFindList(abc, [llGetSubString(src, (index - 1), (index - 1))]) == -1)
       && (llListFindList(abc, [llGetSubString(src, (index + 1), (index + 1))]) == -1))
       {
           src = llDeleteSubString(src, index, index);
           result += ("I" + llGetSubString(src, index, (llSubStringIndex(src, "i") - 1)));
       }
       else
       {
           src = llDeleteSubString(src, index, index);
           result += ("i" + llGetSubString(src, index, (llSubStringIndex(src, "i") - 1)));
       }
   }
   names = llParseString2List(llStringTrim((result = (ko + result)), STRING_TRIM), [], punc); // Reusing variable to save bytes.
   lc = 0;
   index = -1;
   ll = llGetListLength(names);
   string ABC = llToUpper(llDumpList2String(abc, ""));
   do
   {
       string entry = llList2String(names, lc);
       do
       {
           ++index;
           result = llToUpper(llGetSubString(entry, index, index));
       }
       while(llSubStringIndex(ABC, result) == -1);
       names = llListReplaceList(names, [llInsertString(llDeleteSubString(entry, index, index), index, result)], lc, lc);
       index = -1;
   }
   while((lc += 2) < ll);
   return llDumpList2String(names, "");

}

string source = "I can't help but wonder; \"What time should I and Sally arrive for dinner\"? For some reason, I like Tori Amos! Should I?";

default {

   state_entry()
   {
       source = llToUpper(source); // For whatever reason...

// source == "I CAN'T HELP BUT WONDER; "WHAT TIME SHOULD I AND SALLY ARRIVE FOR DINNER"? FOR SOME REASON, I LIKE TORI AMOS! SHOULD I?"

       llOwnerSay(ToNormal(source, ["Sally", "Tori Amos"]));

// Return == "Object: I can't help but wonder; "What time should I and Sally arrive for dinner"? For some reason, I like Tori Amos! Should I?"

   }

}</lsl>