|
Very Keynes
LSL is a Virus
Join date: 6 May 2006
Posts: 484
|
08-09-2008 08:07
I just coded this as I needed a simple calculator function for one of my scripts and thought it may be useful to others, as a search turned up only dedicated calculator scripts such as zxcalc, YAC and VeryCalc. It may also be a good tool to let new scripters see some of the power of lists. If anyone has ideas of how to optimise it, please post, as it was a quick copout to walk the whole list 4 times to set precedence and others may have a more efficient method. For shot calculations though it should be fast enougth. integer PubCh = 8; //set the listen channel here
float calc(string args) { integer x; integer y; list l; y = llGetListLength(l = llParseString2List(args,[" "],["+","-","*","/"])); for(x=0;x<y;x++){if("/"==llList2String(l,x))l=llListReplaceList(l,[llList2Float(l,x-1)/llList2Float(l,x+1)],x-1,x+1);} for(x=0;x<y;x++){if("*"==llList2String(l,x))l=llListReplaceList(l,[llList2Float(l,x-1)*llList2Float(l,x+1)],x-1,x+1);} for(x=0;x<y;x++){if("-"==llList2String(l,x))l=llListReplaceList(l,[llList2Float(l,x - 1)-llList2Float(l,x + 1)],x - 1,x + 1);} for(x=0;x<y;x++){if("+"==llList2String(l,x))l=llListReplaceList(l,[llList2Float(l,x - 1)+llList2Float(l,x + 1)],x - 1,x + 1);} return llList2Float(l,0); }
default { state_entry() { llListen(PubCh,"","",""); } listen(integer InpCh, string usr, key uid, string msg) { llOwnerSay((string)calc(msg)); } }
|
|
Haravikk Mistral
Registered User
Join date: 8 Oct 2005
Posts: 2,482
|
08-09-2008 10:51
Is there a reason this has no formatting at all? It's never scripter-friendly  Anyway, right off there is one optimisation, which is to reduce the number of loops to two since multiplications and divisions have higher precedence than plus and minus, but it doesn't really matter what order they're done in. iirc there's a way to do it in a single loop by parsing the string of characters, lets you account for negative numbers etc. too but I can't remember atm how you'd do it. integer PubCh = 9; //set the listen channel here
float calc (string args) { integer x; list l = llParseString2List(args, [" "], ["+","-","*","/"]); integer y = l != [];
for(x = 0; x < y; x++) { if ("*" ==llList2String(l, x)) l = llListReplaceList( l, [llList2Float(l, x - 1) * llList2Float(l, x + 1)], x - 1, x + 1 ); else if ("/" == llList2String(l, x)) l = llListReplaceList( l, [llList2Float(l, x - 1) / llList2Float(l, x + 1)], x - 1, x + 1 ); }
for (x = 0; x < y; x++) { if("+" == llList2String(l, x)) l = llListReplaceList( l, [llList2Float(l, x - 1) + llList2Float(l, x + 1)], x - 1, x + 1 ); else if ("-" == llList2String(l, x)) l = llListReplaceList( l, [llList2Float(l, x - 1) - llList2Float(l, x + 1)], x - 1, x + 1 ); }
return llList2Float(l, 0); }
default { state_entry() { llListen(PubCh, "", llGetOwner(), ""); }
listen(integer InpCh, string usr, key uid, string msg) { llResetTime(); llOwnerSay((string)calc(msg)); llOwnerSay((string)llGetTime() + " seconds"); } }
_____________________
Computer (Mac Pro): 2 x Quad Core 3.2ghz Xeon 10gb DDR2 800mhz FB-DIMMS 4 x 750gb, 32mb cache hard-drives (RAID-0/striped) NVidia GeForce 8800GT (512mb)
|
|
Very Keynes
LSL is a Virus
Join date: 6 May 2006
Posts: 484
|
08-09-2008 12:25
[ From: Haravikk Mistral Is there a reason this has no formatting at all? It's never scripter-friendly  True  It was part of a much larger script, so I tend to minimise white space and number of lines taken once a function is tested, just so that it takes minimum screen space whilst I’m working on the next function. I should have expanded it out again to post, especially as I thought it may help understand lists. I will remember next time I hope. Thanks, that’s a 50% efficiency increase in one pass and I love your function to get the list length, haven’t seen that one before, I must remember it.
|