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

LSL Wiki : LibrarySLPrint

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

SLPrint

This is a helper function that works similar to printf. It takes two variables, the string and a list of the data. The recognized substrings ("\%","%s","%i","%f.","%f","%h") are replaced with the data from the list in corresponding order.

%s is a string. For example slprint("hi %s",["bob"]) will return the string "hi bob".
%i is integer form. For example slprint("%i",[123]) will return the string "123".
%f is a float. For example slprint("%f",[1.456]) will return the string "1.45600".
%f.# will convert the float to having # of decimal places. For example slprint("%f.2",[1.4567]) will return the string "1.45".
%h will display an integer in hexidecimal form. For example slprint("%h",[1023]) will return the string "3ff".
%H will display an integer in hexidecimal form. For example slprint("%H",[1023]) will return the string "3FF".
\% can be used to bypass conversion. For example slprint("\\%h",[1023]) will return the string "%h". note: within a script you must use a double backslash
%n a spacer token not inserted in the final string, it is skipped.

A more complicated example:
llWhisper(0, slprint("Script '%s' has %i bytes free (%f.2%).",[llGetScriptName(),llGetFreeMemory(),100 * llGetFreeMemory() / (float)(16*1024)]));
Results in:
Object whispers: Script 'New Script' has 14486 bytes free (88.36%).

This script is not particuarly efficient. If you have optimizations please make them. - Ama Omega

string slprint(string mes, list data)
{
    list parsed = llParseString2List(mes,[],["\\%","%s","%i","%f.","%f","%h","%n","%H"]);
    integer i;
    integer j;
    integer max = llGetListLength(parsed);
    mes = ""; //we use this for the output
    string token;
    for (j=i=0;i<max;i++)
    {
        token = llList2String(parsed,i);
        
        if ("%s" == token)
            mes += llList2String(data,j++);
        else if ("%i" == token)
            mes += (string)((integer)llList2String(data,j++));
        else if ("%f" == token)
            mes += (string)((float)llList2String(data,j++));
        else if ("%f." == token)
        {
            // The next token contains the number of places for the float
            string nextToken = llList2String(parsed, i); //we parse the next token too
            integer t = (integer)nextToken;
            token = (string)((float)llList2String(data,j++));
            if(!llSubStringIndex(nextToken,(string)t))//must be in the zero possition
            {//we made sure of the values location and now we are printing it out the answer and the next token.
                token = llGetSubString(token,0,t + llSubStringIndex(token,".")) + 
                            llDeleteSubString(nextToken, 0, llStringLength((string)t) - 1);
                ++i;
            }
            mes += token;
        }
        else if ("%h" == token)
            mes += int2hex((integer)llList2String(data,j++), "0123456789abcdef");
        else if ("%H" == token)
            mes += int2hex((integer)llList2String(data,j++), "0123456789ABCDEF");
        else if ("\\%" == token)
            mes += "%";
        else if ("%n" != token) // we don't parse "%n"
            mes += token;
    }
    return mes;
}

string int2hex(integer h, string s)
{
    string ret;
    integer g = 32; //this is to keep it from going on forever when given a negative number
    do
    {
        ret = llGetSubString(s, h & 15, h & 15) + ret;
        h = h >> 4;
    }while(h && --g);
    return ret;
}


ScriptLibrary | Examples
There are 3 comments on this page. [Display comments/form]