Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

What am I missing?

Khan Kimban
Registered User
Join date: 19 Oct 2006
Posts: 6
01-09-2007 05:31
Why does the following produce different results when programatically they should produce the sdame result? The only difference is the assignment of integer j.

string test1(string str1, string str2) {
integer i;
integer j;
j = llStringLength(str1) + llStringLength(str2);
j = 30 - j;
for (i=1; i <= j; i++)
str1 = str1 + " ";
str1 = str1 + str2;
return str1;
}

string test2(string str1, string str2) {
integer i;
integer j;
j = 30 - llStringLength(str1) + llStringLength(str2);
for (i=1; i <= j; i++)
str1 = str1 + " ";
str1 = str1 + str2;
return str1;
}

default
{
state_entry()
{

}

touch_start(integer total_number)
{
llSay(0, "test1 = " + test1("abc", "123";));
llSay(0, "test2 = " + test2("abc", "123";));
}
}
Stephen Zenith
Registered User
Join date: 15 May 2006
Posts: 1,029
01-09-2007 05:40
From: Khan Kimban
Why does the following produce different results when programatically they should produce the sdame result? The only difference is the assignment of integer j.

string test1(string str1, string str2) {
integer i;
integer j;
j = llStringLength(str1) + llStringLength(str2);
j = 30 - j;
for (i=1; i <= j; i++)
str1 = str1 + " ";
str1 = str1 + str2;
return str1;
}

string test2(string str1, string str2) {
integer i;
integer j;
j = 30 - llStringLength(str1) + llStringLength(str2);
for (i=1; i <= j; i++)
str1 = str1 + " ";
str1 = str1 + str2;
return str1;
}

default
{
state_entry()
{

}

touch_start(integer total_number)
{
llSay(0, "test1 = " + test1("abc", "123";));
llSay(0, "test2 = " + test2("abc", "123";));
}
}


In your assignments of j, the first is setting

CODE

j = 30 - (llStringLength(str1) + llStringLength(str2));


Which is the same as saying

CODE

j = 30 - llStringLength(str1) - llStringLength(str2);



The second is setting

CODE

j = 30 - llStringLength(str1) + llStringLength(str2);


For any string str2 that has a length longer than zero, the two equations are different.
_____________________
Newgate Ludd
Out of Chesse Error
Join date: 8 Apr 2005
Posts: 2,103
01-09-2007 05:51
Basically comes down to Operator precedence.
Use Brackets to make sure you get the calculation you mean.