Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Are Simpler "if" Statements Possible?

Julian Fate
80's Pop Star
Join date: 19 Oct 2003
Posts: 1,020
03-17-2006 19:38
Right now I'm juggling a lot of strings and it is getting unweildy. This keeps happening:
CODE
if (str == "option1" || str == "option2" || str == "option3" || str == "option4" || str == "option5" || str == "option6" || str == "option7" || str == "option8" str == "option9" str == "option10" str == "option8" str == "option11") {
// Do something.
}
Is there a more efficient syntax? By "efficient" I really mean "better looking". :)

Thanks.
Hugsy Penguin
Sky Junkie
Join date: 20 Jun 2005
Posts: 851
03-17-2006 19:55
llListFindList() might be what you want then although I, personally, don't see anything wrong with what you have if it's reformatted a bit:

CODE

default
{
touch_start(integer total_number)
{
string str = "option1";

//one way
if (str == "option1" ||
str == "option2" ||
str == "option3" ||
str == "option4" ||
str == "option5" ||
str == "option6" ||
str == "option7" ||
str == "option8" ||
str == "option9" ||
str == "option10")
{
llOwnerSay("Item found");
}

//another way
list values = ["option1", "option2", "option3", "option4", "option5",
"option6", "option7", "option8", "option9", "option10"];

if (llListFindList(values, [str]) >= 0)
{
llOwnerSay("Item found again");
}
}
}


HTH,

HP
Julian Fate
80's Pop Star
Join date: 19 Oct 2003
Posts: 1,020
03-17-2006 22:17
Thanks, that's very helpful.
Eloise Pasteur
Curious Individual
Join date: 14 Jul 2004
Posts: 1,952
03-18-2006 02:52
Depending on your list of options and what you can do you can code them smartly.

It might not work for your situation - but imagine you're using a dialog box. That can take 24 characters, but only displays 8/9 as we all know. So you put your option1, option2 etc. into the first 8 characters, then (say) two spaces, then a 'tag' - say "tag".

So you'd actually have "option1 tag", "option2 tag" etc.

Then your if statement can be a wee bit harder but something like:

CODE
if(llGetSubString(msg, -5, -1)=="  tag"))