Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

error in if / else if ... but can't find it in text! is there a limit on # lines?

Brent Upshaw
Registered User
Join date: 13 Feb 2007
Posts: 4
08-31-2007 09:54
Hi -

I hope you may help. I have a really straightforward if / else if with a number of statements. I get a syntax error in the same place in the code, even if I move the statemetns around (so the statemnt that got the syntax error one time is swapped with another statement) and it seems to be the place, not the code. The error comes at line 64 below, or after the else if 22. Is there a limit to how many lines or something? It's a really short, simple script.

ANy help would be greatly appreciated - clearly I'm not anywhere near professional, so I appreciate your patience!

Thanks

Chris (Brent Upshaw)

The code:

default
{
state_entry()
{
llSay( 0, "Script loaded!";);
}

touch_start(integer total_number)
{
// do these instructions when the object is touched.

// choose a random number between 0. and 24.0.
float quote_float = llFrand(24.0);
// recast as integer
integer quote = (integer)quote_float;

//finding and saying a quote
if (quote == 0) {
llSay( 0, "To be, or not to be: that is the question. - (Hamlet, Act III, Scene I).";);
} else if (quote == 1) {
llSay( 0, "Neither a borrower nor a lender be; For loan oft loses both itself and friend, and borrowing dulls the edge of husbandry. - (Hamlet, Act I, Scene III).";);
} else if (quote == 2) {
llSay( 0, "This above all: to thine own self be true. - (Hamlet, Act I, Scene III).";);
} else if (quote == 3) {
llSay( 0, "Though this be madness, yet there is method in 't.. - (Hamlet, Act II, Scene II).";);
} else if (quote == 4) {
llSay( 0, "That it should come to this!. - (Hamlet, Act I, Scene II).";);
} else if (quote == 5) {
llSay( 0, "There is nothing either good or bad, but thinking makes it so. - (Hamlet, Act II, Scene II).";);
} else if (quote == 6) {
llSay( 0, "What a piece of work is man! how noble in reason! how infinite in faculty! in form and moving how express and admirable! in action how like an angel! in apprehension how like a god! the beauty of the world, the paragon of animals! . - (Hamlet, Act II, Scene II).";);
} else if (quote == 7) {
llSay( 0, "The lady doth protest too much, methinks. - (Hamlet, Act III, Scene II).";);
} else if (quote == 8) {
llSay( 0, "In my mind's eye. - (Hamlet, Act I, Scene II).";);
} else if (quote == 9) {
llSay( 0, "A little more than kin, and less than kind. - (Hamlet, Act I, Scene II).";);
} else if (quote == 10) {
llSay( 0, "The play 's the thing wherein I'll catch the conscience of the king. - (Hamlet, Act II, cene II).";);
} else if (quote == 11) {
llSay( 0, "And it must follow, as the night the day, thou canst not then be false to any man. - (Hamlet, Act I, Scene III).";);
} else if (quote == 12) {
llSay( 0, "This is the very ecstasy of love. - (Hamlet, Act II, Scene I).";);
} else if (quote == 13) {
llSay( 0, "Brevity is the soul of wit. - (Hamlet, Act II, Scene II).";);
} else if (quote == 14) {
llSay( 0, "Doubt that the sun doth move, doubt truth to be a liar, but never doubt I love. - (Hamlet, Act II, Scene II).";);
} else if (quote == 15) {
llSay( 0, "Rich gifts wax poor when givers prove unkind. - (Hamlet, Act III, Scene I).";);
} else if (quote == 16) {
llSay( 0, "Do you think I am easier to be played on than a pipe? - (Hamlet, Act III, Scene II).";);
} else if (quote == 17) {
llSay( 0, "I will speak daggers to her, but use none. - (Hamlet, Act III, Scene II).";);
} else if (quote == 18) {
llSay( 0, "When sorrows come, they come not single spies, but in battalions. - (Hamlet, Act IV, Scene V).";);
} else if (quote == 19) {
llSay( 0, "All the world 's a stage, and all the men and women merely players. They have their exits and their entrances - And one man in his time plays many parts - (As You Like It, Act II, Scene VII).";);
} else if (quote == 20) {
llSay( 0, "Can one desire too much of a good thing?. - (As You Like It, Act IV, Scene I).";);
} else if (quote == 21) {
llSay( 0, "I like this place and willingly could waste my time in it - (As You Like It, Act II, Scene IV).";);
} else if (quote == 22) {
llSay( 0, "How bitter a thing it is to look into happiness through another man's eyes! - (As You Like It, Act V, Scene II).";);
} else if (quote == 23) {
llSay( 0, "Blow, blow, thou winter wind! Thou art not so unkind as man's ingratitude. - (As You Like It, Act II, Scene VII).";);
} else if (quote == 24) {
llSay( 0, "True is it that we have seen better days. - (As You Like It, Act II, Scene VII). ";);
}

}
}
Magrell Wise
Registered User
Join date: 19 Jan 2007
Posts: 23
08-31-2007 10:12
There is a limit to the number of ELSE clauses you can have (I think 20). You may want to nest it half-way through:

if (x == 1)
:
else if (x == 10)
:
else
{
if (x == 11)
:
else if (...
}
Ravanne Sullivan
Pole Dancer Extraordinair
Join date: 10 Dec 2005
Posts: 674
08-31-2007 10:12
Yes there is a limit to the level of nesting for if else statements. I have heard it put at 23 but I have found that there are often problems at much lower levels depending on the amount of memory used by your script.

A simpler way to write your program might be to use a list containing the various phrases and then just pick the desired element from the list.


----

list QuoteList = ["To be, or not to be: that is the question. - (Hamlet, Act III, Scene I).", ... "True is it that we have seen better days. - (As You Like It, Act II, Scene VII). ";)];

default
{
state_entry()
{
llSay( 0, "Script loaded!";);
}

touch_start(integer total_number)
{
// do these instructions when the object is touched.

// choose a random number between 0. and 24.0.
float quote_float = llFrand(24.0);
// recast as integer
integer quote = (integer)quote_float;

llSay(0,llList2String(QuoteList, quote));
}
}
_____________________
Ravanne's Dance Poles and Animations

Available at my Superstore and Showroom on Insula de Somni
http://slurl.com/secondlife/Insula de Somni/94/194/27/
Darien Caldwell
Registered User
Join date: 12 Oct 2006
Posts: 3,127
08-31-2007 12:31
From my experience it is around 12-13 if-elses before the compiler chokes. But your mileage may vary. :)
_____________________
Masakazu Kojima
ケロ
Join date: 23 Apr 2004
Posts: 232
08-31-2007 16:28
http://forums.secondlife.com/showthread.php?t=163711#post1398422
http://w-hat.com/stackdepth
Jesse Barnett
500,000 scoville units
Join date: 21 May 2006
Posts: 4,160
08-31-2007 17:31
It's not only stack depth but also script performance that should be a concern when doing multiple if, else if tests. Here are a couple of threads where some performance testing was done in the past:

/54/cf/148598/1.html

/54/02/144383/1.html
_____________________
I (who is a she not a he) reserve the right to exercise selective comprehension of the OP's question at anytime.
From: someone
I am still around, just no longer here. See you across the aisle. Hope LL burns in hell for archiving this forum
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
09-01-2007 07:50
It's a stack depth problem in the compiler. I don't know why LL doesn't fix it.
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey
Lee Ludd
Scripted doors & windows
Join date: 16 May 2005
Posts: 243
09-01-2007 11:47
I think the problem is due to memory constraints, so there's probably no fixed limit on the depth of code.

I code what in a better language would be a switch, or a series of "if...; else if ...; sequences this way:

if ( condition1)
{
do something
return;
}

if ( condition2)
{
do something
return;
}

etc. etc.
Jotheph Nemeth
Registered User
Join date: 9 Aug 2007
Posts: 142
09-01-2007 15:13
You could listen to these guys, OR you could eliminate most of those if statements entirely.

list quotelist =[
"To be, or not to be: that is the question. - (Hamlet, Act III, Scene I).",
"Neither a borrower nor a lender be; For loan oft loses both itself and friend, and borrowing dulls the edge of husbandry. - (Hamlet, Act I, Scene III).",
"This above all: to thine own self be true. - (Hamlet, Act I, Scene III).",
"Though this be madness, yet there is method in 't.. - (Hamlet, Act II, Scene II).",
"That it should come to this!. - (Hamlet, Act I, Scene II).",
"There is nothing either good or bad, but thinking makes it so. - (Hamlet, Act II, Scene II).",
"What a piece of work is man! how noble in reason! how infinite in faculty! in form and moving how express and admirable! in action how like an angel! in apprehension how like a god! the beauty of the world, the paragon of animals! . - (Hamlet, Act II, Scene II).",
"The lady doth protest too much, methinks. - (Hamlet, Act III, Scene II).",
"In my mind's eye. - (Hamlet, Act I, Scene II).",
"A little more than kin, and less than kind. - (Hamlet, Act I, Scene II).",
"The play 's the thing wherein I'll catch the conscience of the king. - (Hamlet, Act II, cene II).",
"And it must follow, as the night the day, thou canst not then be false to any man. - (Hamlet, Act I, Scene III).",
"This is the very ecstasy of love. - (Hamlet, Act II, Scene I).",
"Brevity is the soul of wit. - (Hamlet, Act II, Scene II).",
"Doubt that the sun doth move, doubt truth to be a liar, but never doubt I love. - (Hamlet, Act II, Scene II).",
"Rich gifts wax poor when givers prove unkind. - (Hamlet, Act III, Scene I).",
"Do you think I am easier to be played on than a pipe? - (Hamlet, Act III, Scene II).",
"I will speak daggers to her, but use none. - (Hamlet, Act III, Scene II).",
"When sorrows come, they come not single spies, but in battalions. - (Hamlet, Act IV, Scene V).",
"All the world 's a stage, and all the men and women merely players. They have their exits and their entrances - And one man in his time plays many parts - (As You Like It, Act II, Scene VII).",
"Can one desire too much of a good thing?. - (As You Like It, Act IV, Scene I).",
"I like this place and willingly could waste my time in it - (As You Like It, Act II, Scene IV).",
"How bitter a thing it is to look into happiness through another man's eyes! - (As You Like It, Act V, Scene II).",
"Blow, blow, thou winter wind! Thou art not so unkind as man's ingratitude. - (As You Like It, Act II, Scene VII).",
"True is it that we have seen better days. - (As You Like It, Act II, Scene VII). "
];


touch_start(integer total_number)
{
// do these instructions when the object is touched.

// choose a random number between 0. and 24.0.
float quote_float = llFrand(24.0);
// recast as integer
integer quote = (integer)quote_float;

llSay(0, llList2String(quotelist, quote));
}


Lists take memory, but so do if statements. I expect these two balance out.
There is a limit to how many items you can have in a list, but you can get around this by breaking it into 2 or more lists.

for example:
if (quote < 20) {
llSay(0, llList2String(quotelist1, quote));
} else if (quote >= 20 and quote < 40) {
llSay(0, llList2String(quotelist2, quote-20));
}
Ravanne Sullivan
Pole Dancer Extraordinair
Join date: 10 Dec 2005
Posts: 674
09-01-2007 23:37
"Holy redunancy, Batman!"

It might help to read the previous posts before you duplicate an answer already given.
_____________________
Ravanne's Dance Poles and Animations

Available at my Superstore and Showroom on Insula de Somni
http://slurl.com/secondlife/Insula de Somni/94/194/27/
Jotheph Nemeth
Registered User
Join date: 9 Aug 2007
Posts: 142
09-01-2007 23:52
From: Ravanne Sullivan
"Holy redunancy, Batman!"

It might help to read the previous posts before you duplicate an answer already given.


Actually, I did read the previous posts. However, I did miss the second part of your post.

My mistake, but I was tired and I was reading all the rest of the posts about how to fix the symptom of nested if statements that followed yours. My mind saw it but wrongly recognized it as a huge signature.

However, I did include an extra option that you hadn't, in case the list gets too big.

Btw, thanks much for being so sarcastic about it rather than simply saying "hey, I already posted that method above. Second post. Just so you know."

Gee. Now I feel all warm and welcome here and appreciated for trying to contribute. Do you kick visitors in the shin at parties because they brought the same kind of beer you did?
Jesse Barnett
500,000 scoville units
Join date: 21 May 2006
Posts: 4,160
09-02-2007 05:59
From: Jotheph Nemeth
Btw, thanks much for being so sarcastic about it rather than simply saying "hey, I already posted that method above. Second post. Just so you know."

Gee. Now I feel all warm and welcome here and appreciated for trying to contribute. Do you kick visitors in the shin at parties because they brought the same kind of beer you did?


Actually to be fair you asked for it when you made this statement:

"You could listen to these guys, OR you......................."
_____________________
I (who is a she not a he) reserve the right to exercise selective comprehension of the OP's question at anytime.
From: someone
I am still around, just no longer here. See you across the aisle. Hope LL burns in hell for archiving this forum
Jesse Barnett
500,000 scoville units
Join date: 21 May 2006
Posts: 4,160
09-02-2007 06:47
Now back to the meat & potatoes..........

As we learn to script we should be forever learning and striving to save on sim resources by coding in such a fashion to lessen the impact of our scripts. If, Else & Else If tests are horrible resource hogs if coded wrongly.

If you go with the OP's script which are straight Else If's then not only will it not compile but if you were able to run it would take about 5,800 bytes of memory and from 0.02 seconds to run if it was the first If to well over 6 seconds to give the results if it was the last 24th Else If.

If you go with nested Else If's it will take about the same memory and will take from 0.02 to about a second or so to run to the last.

A binary Else If script is very efficient and will take the same memory but will return the results in 0.02 seconds to about 0.044 seconds. But it is a pain to code. It is still the best option if you do have to use a lot of Else If's

But in this case you can code it as Ravanne and Joteph pointed out. It is easier to write, much more readable and the most effiecient by far:

"Object: 0.022180 elapsed time & 2869 bytes"

CODE

list quotelist =[
"To be, or not to be: that is the question. - (Hamlet, Act III, Scene I).",
"Neither a borrower nor a lender be; For loan oft loses both itself and friend, and borrowing dulls the edge of husbandry. - (Hamlet, Act I, Scene III).",
"This above all: to thine own self be true. - (Hamlet, Act I, Scene III).",
"Though this be madness, yet there is method in 't.. - (Hamlet, Act II, Scene II).",
"That it should come to this!. - (Hamlet, Act I, Scene II).",
"There is nothing either good or bad, but thinking makes it so. - (Hamlet, Act II, Scene II).",
"What a piece of work is man! how noble in reason! how infinite in faculty! in form and moving how express and admirable! in action how like an angel! in apprehension how like a god! the beauty of the world, the paragon of animals! . - (Hamlet, Act II, Scene II).",
"The lady doth protest too much, methinks. - (Hamlet, Act III, Scene II).",
"In my mind's eye. - (Hamlet, Act I, Scene II).",
"A little more than kin, and less than kind. - (Hamlet, Act I, Scene II).",
"The play 's the thing wherein I'll catch the conscience of the king. - (Hamlet, Act II, cene II).",
"And it must follow, as the night the day, thou canst not then be false to any man. - (Hamlet, Act I, Scene III).",
"This is the very ecstasy of love. - (Hamlet, Act II, Scene I).",
"Brevity is the soul of wit. - (Hamlet, Act II, Scene II).",
"Doubt that the sun doth move, doubt truth to be a liar, but never doubt I love. - (Hamlet, Act II, Scene II).",
"Rich gifts wax poor when givers prove unkind. - (Hamlet, Act III, Scene I).",
"Do you think I am easier to be played on than a pipe? - (Hamlet, Act III, Scene II).",
"I will speak daggers to her, but use none. - (Hamlet, Act III, Scene II).",
"When sorrows come, they come not single spies, but in battalions. - (Hamlet, Act IV, Scene V).",
"All the world 's a stage, and all the men and women merely players. They have their exits and their entrances - And one man in his time plays many parts - (As You Like It, Act II, Scene VII).",
"Can one desire too much of a good thing?. - (As You Like It, Act IV, Scene I).",
"I like this place and willingly could waste my time in it - (As You Like It, Act II, Scene IV).",
"How bitter a thing it is to look into happiness through another man's eyes! - (As You Like It, Act V, Scene II).",
"Blow, blow, thou winter wind! Thou art not so unkind as man's ingratitude. - (As You Like It, Act II, Scene VII).",
"True is it that we have seen better days. - (As You Like It, Act II, Scene VII). "
];

default {
touch_start(integer total_number)
{
llResetTime();
//float quote_float = llFrand(24.0);
// recast as integer
//integer quote = (integer)quote_float;

//No need to recast

integer quote = (integer)llFrand(24.0);
llSay(0, llList2String(quotelist, quote));
float elapsed = llGetTime();
integer mem = 16000 - llGetFreeMemory();
llSay(DEBUG_CHANNEL, (string)elapsed + " elapsed time" + " & " + (string)mem + " bytes");
}
}
_____________________
I (who is a she not a he) reserve the right to exercise selective comprehension of the OP's question at anytime.
From: someone
I am still around, just no longer here. See you across the aisle. Hope LL burns in hell for archiving this forum
Jotheph Nemeth
Registered User
Join date: 9 Aug 2007
Posts: 142
09-02-2007 13:47
From: Jesse Barnett
Actually to be fair you asked for it when you made this statement:

"You could listen to these guys, OR you......................."


Yes, as in fix the IF statements or just get rid of 90% of them.

There was no sarcasm there against anyone.

But I understand your point. I could have phrased it better myself, such as "you could fix your IF statements or you could get rid of most of them this way:".
Ravanne Sullivan
Pole Dancer Extraordinair
Join date: 10 Dec 2005
Posts: 674
09-02-2007 14:55
From: Jotheph Nemeth

Btw, thanks much for being so sarcastic about it rather than simply saying "hey, I already posted that method above. Second post. Just so you know."


Sorry, it was not my intention to be sarcastic I was trying for funny and guess I didn't pull it off too well.

And no, I don't kick anyone in the shins for bringing the same beer as I do since I don't drink I don't tend to bring beer. But if you steal my shrimp dip recipe, Bring it on!
_____________________
Ravanne's Dance Poles and Animations

Available at my Superstore and Showroom on Insula de Somni
http://slurl.com/secondlife/Insula de Somni/94/194/27/
Jotheph Nemeth
Registered User
Join date: 9 Aug 2007
Posts: 142
09-03-2007 08:26
From: Ravanne Sullivan
Sorry, it was not my intention to be sarcastic I was trying for funny and guess I didn't pull it off too well.

And no, I don't kick anyone in the shins for bringing the same beer as I do since I don't drink I don't tend to bring beer. But if you steal my shrimp dip recipe, Bring it on!


No biggie. I was just being a bit over-sensitive from something else I think.