Difference between revisions of "User:ANSI Soderstrom/Misc useful functions"

From Second Life Wiki
Jump to navigation Jump to search
Line 11: Line 11:
// returns a string with all replacements for "modifier".... based on the equivalent positions in the list-content
// returns a string with all replacements for "modifier".... based on the equivalent positions in the list-content
string printf(string sentence, list insertation) {
string printf(string sentence, list insertation) {
    string modifier = "%s";
     if(llGetListLength(insertation)) {
     if(llGetListLength(insertation)) {
        string modifier = "%s";
         integer i;
         integer i;
         integer match;
         integer match;
Line 18: Line 18:
             match = llSubStringIndex(sentence,modifier);
             match = llSubStringIndex(sentence,modifier);
             if(~match) {
             if(~match) {
                 sentence = llDeleteSubString(sentence,match,match+1);
                 sentence = llDeleteSubString(sentence,match,match+llStringLength(modifier)-1);
                 sentence = llInsertString(sentence,match,llList2String(insertation,i));
                 sentence = llInsertString(sentence,match,llList2String(insertation,i));
             }
             }

Revision as of 04:37, 12 February 2011

Leave a comment

Misc useful functions

Some are useful, some not...

<lsl>

// modify a string in ANSI-C style // returns a string with all replacements for "modifier".... based on the equivalent positions in the list-content string printf(string sentence, list insertation) {

   string modifier = "%s";
   if(llGetListLength(insertation)) {
       integer i;
       integer match;
       for(i=0;i<llGetListLength(insertation);++i) {
           match = llSubStringIndex(sentence,modifier);
           if(~match) {
               sentence = llDeleteSubString(sentence,match,match+llStringLength(modifier)-1);
               sentence = llInsertString(sentence,match,llList2String(insertation,i));
           }
       }
       return sentence;
   } 
   return "Error, i miss some \"" + modifier + "\" or the list is empty for sentence: " + sentence;

}

default {

   state_entry() {
       llOwnerSay(printf("Hello %s, How %s you?\n%s is a number.",["Avatar","are",3]));
       // Output:
       // Hello Avatar, How are you? 
       // 3 is a number.
   }

} </lsl>