Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

string comparison

Gregory McLeod
Registered User
Join date: 21 Oct 2006
Posts: 278
08-01-2007 11:40
Thus will probably result in that Doh! moment yet again.

In a script I have two string variables. They are both the same length 10 characters and derived from bigger strings using llGetSubString(... etc

The variables display using llOwnerSay as
0000000000 that is ten zeroes.

When I use them in an if statement thus
if (Var1 == Var2)
{
... do something;
}
the condition is never interpreted as TRUE and the something is never done.
Is there something strange about these strings or LSL?
Talarus Luan
Ancient Archaean Dragon
Join date: 18 Mar 2006
Posts: 4,831
08-01-2007 12:21
In LSL, two strings are considered equal if their contents match /exactly/. IE, they have to be the same length, and each character in each position must match in both.

My guess is that you have a bug in your code which is causing one string to be longer or shorter than the other, or you are picking characters from the wrong position.

Otherwise, I would have to see the code in question to discern the problem. String comparisons work fine as far as I have seen (and I use a LOT of them).
Meade Paravane
Hedgehog
Join date: 21 Nov 2006
Posts: 4,845
08-01-2007 12:26
/me bets on an "=" instead of an "==".

Uh... Unless that always evaulates to true. Then I bet on something else.
_____________________
Tired of shouting clubs and lucky chairs? Vote for llParcelSay!!!
- Go here: http://jira.secondlife.com/browse/SVC-1224
- If you see "if you were logged in.." on the left, click it and log in
- Click the "Vote for it" link on the left
uzi Under
The Card Lady
Join date: 14 Mar 2005
Posts: 31
08-01-2007 12:47
My money's on = instead of == as well (done that SOOOO many times).

Maybe spaces or chars you didnt spot with the Say. I'd try a
llOwnerSay("="+Var1+"="+Var2+"=";);
immediately before the If to check.

- U2
_____________________
The Card Lady

CardCity in Scaramanga to order greetings cards and gifts; chose a delivery date with personal messages. Order tracking at www.mostsuper.com/cardcity
Shadow Subagja
Registered User
Join date: 29 Apr 2007
Posts: 354
08-01-2007 12:52
print the orignal strings, the concatenated strings, and the llGetSubString commands you used here. Oh and for testing, don't make them all 00's, use something that you can spot an error in.

From: Gregory McLeod
Thus will probably result in that Doh! moment yet again.

In a script I have two string variables. They are both the same length 10 characters and derived from bigger strings using llGetSubString(... etc

The variables display using llOwnerSay as
0000000000 that is ten zeroes.

When I use them in an if statement thus
if (Var1 == Var2)
{
... do something;
}
the condition is never interpreted as TRUE and the something is never done.
Is there something strange about these strings or LSL?
Talarus Luan
Ancient Archaean Dragon
Join date: 18 Mar 2006
Posts: 4,831
08-01-2007 13:15
I haven't done the =/== in a long time. Mainly because I instinctively double-check every time, and always use == by habit in conditionals, and have to go back and change it to = if that is what I meant to do (though rarely, if ever; assignments in conditionals just feel _wrong_ to me ;) ).
Gregory McLeod
Registered User
Join date: 21 Oct 2006
Posts: 278
08-01-2007 14:43
OK here is an extract of the code

string SetupCI; // initial piece colors
string SetupSI; // initial piece status
string SetupCW; // working copy of piece colors
string SetupSW; // working copy of piece status
string Allc = "cccccccccc";
string All0 = "0000000000";
string All1 = "1111111111";
string All2 = "2222222222";
string All3 = "3333333333";
string All4 = "4444444444";
string All5 = "5555555555";
string C02_11;
string C12_21;
string C22_31;
string C32_41;
string C42_51;
string C52_61;
string S02_11;
string S12_21;
string S22_31;
string S32_41;
string S42_51;
string S52_61;
// Functions
integer CheckWin()
{
C02_11 = llGetSubString(SetupCW,2,11);
C12_21 = llGetSubString(SetupCW,12,21);
C22_31 = llGetSubString(SetupCW,22,31);
C32_41 = llGetSubString(SetupCW,32,41);
C42_51 = llGetSubString(SetupCW,42,51);
C52_61 = llGetSubString(SetupCW,52,61);
S02_11 = llGetSubString(SetupSW,2,11);
S12_21 = llGetSubString(SetupSW,12,21);
S22_31 = llGetSubString(SetupSW,22,31);
S32_41 = llGetSubString(SetupSW,32,41);
S42_51 = llGetSubString(SetupSW,42,51);
S52_61 = llGetSubString(SetupSW,52,61);
if ((C02_11 == Allc) && (S02_11 == All3))
{
return 1;
}else
if ((C12_21 == Allc) && (S12_21 == All4))
{
return 1;
}else
if ((C22_31 == Allc) && (S22_31 == All5))
{
return 1;
}else
llOwnerSay("C32_41: " + C32_41);
llOwnerSay("S32_41: " + S32_41);
llOwnerSay("Allc: " + Allc);
llOwnerSay("All0: " + All0);
if (C32_41 == Allc)
{
llOwnerSay("C32_41 = Allc";);
}
if (S32_41 == All0)
{
llOwnerSay("S32_41 = All0";);
}
if ((C42_51 == Allc) && (S42_51 == All1))
{
return 1;
}else
if ((C52_61 == Allc) && (S52_61 == All2))
{
return 1;
}else
{
return 0;
}
} // end of CheckWin function

Init()
{
SetupCI = "99"; // pieces 0 - 1
SetupCI += "0000000000111111111122222222223333333333"; // pieces 2 - 41
SetupCI += "4444444444555555555566666666666666666666"; // pieces 42 - 81
SetupCI += "66666666666666666666666666666666666666666"; // pieces 82 - 122
SetupSI = "zz"; // pieces 0 - 1
SetupSI += "cccccccccccccccccccccccccccccccccccccccc"; // pieces 2 - 41
SetupSI += "cccccccccccccccccccceeeeeeeeeeeeeeeeeeee"; // pieces 42 - 81
SetupSI += "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"; // pieces 82 - 122
SetupCW = SetupCI; // working copy of piece colors
SetupSW = SetupSI; // working copy of piece status
} // end of Init function

// States
default
{
state_entry()
{
Init();
llListen(1234, "", NULL_KEY, "";);
}

touch_start(integer total_number)
{
}

listen(integer channel, string name, key id, string message)
{
if (channel == 1234)
{
if (message == "Memory";)
{
llOwnerSay("Free memory is " + (string)llGetFreeMemory());
}else
if (message == "Checkstate";)
{
llOwnerSay("Play.Checkstate: " + SetupSW + SetupCW);
}else
if (message == "Loadstate";)
{
SetupCW = "99"; // pieces 0 - 1
SetupCW += "6666666666666666666666666666660000000000"; // pieces 2 - 41
SetupCW += "6666666666666666666666666666666666660666"; // pieces 42 - 81
SetupCW += "66666666666666666666666666666666666666666"; // pieces 82 - 122
SetupSW = "zz"; // pieces 0 - 1
SetupSW += "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeecccccccccc"; // pieces 2 - 41
SetupSW += "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeceee"; // pieces 42 - 81
SetupSW += "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"; // pieces 82 - 122
llOwnerSay("Loaded";);
}else
if (message == "Checkit";)
{
integer result = CheckWin();
llOwnerSay("Result: " + (string)result);
}
} // end of 1234 block
} // end of listen block
} // end of default state

put it in a box when saved execute /1234Loadstate
then /1234Checkit.

Comments accepted.
Talarus Luan
Ancient Archaean Dragon
Join date: 18 Mar 2006
Posts: 4,831
08-01-2007 14:57
First impression: Looks like you are mixing up your colors and your status comparisons.

You are comparing your color variables to Allc and your status variables to All0-All5. Assuming you don't want to swap the meaning of the data you store in them, that is probably why they never compare as equal anywhere in that code.

Programmer's Rule #4: Use meaningful variable names to prevent confusing *yourself*, let alone every poor sucker who has to come after you to maintain your code. ;)
Anthalia Nemeth
Registered User
Join date: 8 Mar 2007
Posts: 17
08-01-2007 15:26
I just threw your code into LSLEditor and put some debug lines into your CheckWin function. Here's the result:

new: Loaded
new: ((C02_11 == Allc) && (S02_11 == All3)): 6666666666 == cccccccccc && eeeeeeeeee == 3333333333
new: ((C12_21 == Allc) && (S12_21 == All4)): 6666666666 == cccccccccc && eeeeeeeeee == 4444444444
new: ((C22_31 == Allc) && (S22_31 == All5)): 6666666666 == cccccccccc && eeeeeeeeee == 5555555555
new: C32_41: 0000000000
new: S32_41: cccccccccc
new: Allc: cccccccccc
new: All0: 0000000000
new: (C32_41 == Allc): 0000000000 == cccccccccc
new: (S32_41 == All0): cccccccccc == 0000000000
new: ((C42_51 == Allc) && (S42_51 == All1)): 6666666666 == cccccccccc && eeeeeeeeee == 1111111111
new: ((C52_61 == Allc) && (S52_61 == All2)): 6666666666 == cccccccccc && eeeeeeeeee == 2222222222
new: Result: 0


I don't know what you're really trying to do, but hope that helps ya!

A simple function like:
CODE

integer debug = TRUE;
db(string msg)
{
if (debug == TRUE) llOwnerSay(msg);
}

can be an imense help when debugging code.
Gregory McLeod
Registered User
Join date: 21 Oct 2006
Posts: 278
08-02-2007 00:54
Thanks to all the contributors.

DOH!

I knew it would be something simple. I did mix up my comparisons Allc is to check whether a piece is colored, dimmed, flashing or empty and All0-5 is to check which color.
At the end of the game the winner's pieces must be all in the right place and the right color.

I just got them mixed.

Thanks again for sorting me out.