Jump to content

No ternary (or equiv)?


LoneWolfiNTj
 Share

You are about to reply to a thread that has been inactive for 946 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

I just got through doing an extreme editing job on a script I found in a full-perms security orb. It was buggy, and full of features I didn't need, and missing features I did need. So I fixed it. 🙂 Took several hours but I learned a lot, so it was worth it.

In the process I came across several places involving code duplication in if/else statements. To give a simplified example:

status_text()
{
    string sta_str;
    if(status) {sta_str = "Status: On" ;}
    else       {sta_str = "Status: Off";}
    llSetText(sta_str, <0.0,1.0,0.0>, 1.0);
}

Ok, that works, but I figured I'd get it down from 4 lines to 2 by using a ternary operator:

text()
{
    string sta_str = "Status: " + (status?"On":"Off");
    llSetText(sta_str, <0.0,1.0,0.0>, 1.0);
}

But alas that gave "syntax error". No ternary ("a?b:c") operator in LSL, alas. Too bad, because many other languages (C, C++, Perl, etc) have it and it's very useful.

So, is there any other similar feature? Or are with stuck with if / else?

Link to comment
Share on other sites

You are indeed stuck with if/else. However, the original code can be made one line shorter:

status_text()
{
    string sta_str = "Status: Off";
    if(status) sta_str = "Status: On" ;
    llSetText(sta_str, <0.0,1.0,0.0>, 1.0);
}

By assigning a default value to sta_str, the value will be retained when the the condition (status) is FALSE.
You also usually don't need the accolades (curly brackets) when an if-statement contains only one function to be executed when the condition is TRUE, so I removed that too in the above example.

  • Like 1
Link to comment
Share on other sites

16 minutes ago, Fritigern Gothly said:

You also usually don't need the accolades (curly brackets) when an if-statement contains only one function to be executed when the condition is TRUE

That's true, but it's a bad habit to get into. Always use the curly brackets to delineate the scope of a conditional block of code, even when they aren't technically necessary.  That way, you'll be less likely to forget them when they are required (which is most of the time).

  • Like 1
Link to comment
Share on other sites

1 hour ago, Fritigern Gothly said:
status_text()
{
    string sta_str = "Status: Off";
    if(status) sta_str = "Status: On" ;
    llSetText(sta_str, <0.0,1.0,0.0>, 1.0);
}

By assigning a default value to sta_str, the value will be retained when the the condition (status) is FALSE.

 

this is the fastest way to do it.  Is about 2 times faster than If Else and about 10 times faster than llList2String(["Status: Off", "Status: On"], status);

  • Like 2
Link to comment
Share on other sites

4 hours ago, Rolig Loon said:

Always use the curly brackets to delineate the scope of a conditional block of code, even when they aren't technically necessary.

I generally agree, but in my current mental formatting guidelines for myself(which I don't always follow. . .) , I'm allowing a single statement after an if without curly brackets if it fits on one line.

// bad formatting:
if(ID==llGetOwner())
  return;

// ok formatting:
if(ID==llGetOwner()) return;

 

  • Thanks 1
Link to comment
Share on other sites

if there is an authority on LSL code formatting it would be the original Linden Scripting Language Guide 2003 as the originators wrote it up.

from page 12
 

4.1. Conditional Statements
The ’if’ statement operates and has the same syntax as the Java/C version.

check_message(string message)
{
    if(message == "open")
    {
        open();
    }
    else if(message == "close")
    {
        close();
    }
    else
    {
        llSay(0, "Unknown command: " + message);
    }
}

is not quite Java/C as there is no exact standard for Java or C. But LSL follows BSD Allman style fairly closely

link to a pdf copy of LSL Guide 2003 is here: https://economicslab.tistory.com/attachment/cfile3.uf@1909190B49D79475010A44.pdf/

 

 

  • Like 1
Link to comment
Share on other sites

16 hours ago, LoneWolfiNTj said:

Ok, that works, but I figured I'd get it down from 4 lines to 2 by using a ternary operator:

text()
{
    string sta_str = "Status: " + (status?"On":"Off");
    llSetText(sta_str, <0.0,1.0,0.0>, 1.0);
}

But alas that gave "syntax error". No ternary ("a?b:c") operator in LSL, alas. Too bad, because many other languages (C, C++, Perl, etc) have it and it's very useful.

So, is there any other similar feature? Or are with stuck with if / else?

There is no ternary operator in LSL, but if you want to avoid repetition, you could do some dumb list trickery.

string sta_str = "Status: " + llList2String(["Off", "On"], status);

 

Edited by Wulfie Reanimator
  • Like 2
Link to comment
Share on other sites

3 hours ago, Quistess Alpha said:

Wow, Interesting to see how much was already there in the beginning. I would have placed my bet that the vehicle functions were a latter addition.

vehicles came toward the end of 2003. Cory, Ben and Andrew Linden made some example scripts at the time showing the vehicle functions. Like this one: Simple Airplane example dated 12-08-2003. https://www.outworldz.com/cgi/freescripts.plx?ID=757 

  • Like 1
Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 946 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...