|
Devious Lei
Registered User
Join date: 13 Sep 2006
Posts: 3
|
10-15-2006 11:54
What I am trying to do is make if then statements that can compare letters using the less than and greater then signs. To make it clearer here is an example. string MyVariable = "d";
If (MyVariable < "f") { // do this } else if (MyVariable < "t") { // do this }
The problem ofcourse is that I can't seem to find out how to compare them like that. The only operator I can use is the '=='. I could just simply make the code run through tons of if then statments asking "is myvariable == ALPHA" but that wouldn't be as efficent as trying to find a range that the letter might be between. One thing I tried was converting the String characters into integer values but that didn't work. When you take "t" and type cast it as an integer it just equals 0. There has to be someway to do this so if anyone has any ideas please help me out.
|
|
Jesse Barnett
500,000 scoville units
Join date: 21 May 2006
Posts: 4,160
|
10-15-2006 12:37
What about a list containing all the letters in alphabetical order. Then just compare the positions of the letters. Actually you could do it with a string also, would probably be easier but the same setup string containing all the letters in alphabeticla order. Look here in the wiki to use a string: http://www.lslwiki.com/lslwiki/wakka.php?wakka=llSubStringIndex
_____________________
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
|
10-15-2006 13:00
Here it is done with a list: list variable_letter = ["e"]; list set_letter = ["c"]; list alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i"];
default { touch_start(integer num_detected) { integer variable = llListFindList(alphabet, variable_letter); integer set = llListFindList(alphabet, set_letter);
if (variable > set) llOwnerSay((string)variable_letter + " comes after " + (string)set_letter);
if (variable<set) llOwnerSay((string)variable_letter + " comes before " + (string)set_letter);
if (variable == set) llOwnerSay("They are both the same"); } }
Sorry too lazy right now to do it with a string but you get the idea now 
_____________________
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
|
|
Newgate Ludd
Out of Chesse Error
Join date: 8 Apr 2005
Posts: 2,103
|
10-15-2006 13:05
I dont know if its any more or less efficient but I've used integer llSubStringIndex(string source, string pattern) to do the same thing.
|
|
Haravikk Mistral
Registered User
Join date: 8 Oct 2005
Posts: 2,482
|
10-15-2006 14:55
llSubStringIndex ought to be about the same speed, but it'd certainly be more memory efficient to have it as a string than a list which counts for something I think, so I'd prefer the sub-string method personally.
It would be nice to have chars like in C, e.g a new datatype 'char', if you cast a string to a char then it takes the first letter. A char is then simply an integer masquerading as a text character so that you can do greater than etc. on it.
_____________________
Computer (Mac Pro): 2 x Quad Core 3.2ghz Xeon 10gb DDR2 800mhz FB-DIMMS 4 x 750gb, 32mb cache hard-drives (RAID-0/striped) NVidia GeForce 8800GT (512mb)
|
|
Cid Jacobs
Theoretical Meteorologist
Join date: 18 Jul 2004
Posts: 4,304
|
10-16-2006 00:19
string variable_letter = "c"; string set_letter = "j"; string alpha_numeric = "abcdefghijklmnopqrstuvwxyz";
default { touch_start(integer num_detected) { integer variable = llSubStringIndex(alpha_numeric, variable_letter); integer set = llSubStringIndex(alpha_numeric, set_letter); if (variable > set) { llOwnerSay((string)variable_letter + " comes after " + (string)set_letter); } if (variable<set) { llOwnerSay((string)variable_letter + " comes before " + (string)set_letter); } if (variable == set) { llOwnerSay("They are both the same"); } } }
|
|
Devious Lei
Registered User
Join date: 13 Sep 2006
Posts: 3
|
10-16-2006 16:57
Thanks
|
|
Newgate Ludd
Out of Chesse Error
Join date: 8 Apr 2005
Posts: 2,103
|
STRCMP, STRNCMP , STRICMP and STRNICMP
10-19-2006 09:51
Incase anyone wants them here are my strcmp functiond for lsl... // // String Compare functions for LSL. // // string characters = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
integer STRINVALID = -99999; integer STREQUAL = 0; integer STRLESSTHAN = -1; integer STRGREATERTHAN = 1;
integer strncmp(string a, string b,integer len) { // compares two strings character by character. // return as soon as there is a difference integer result = STREQUAL; if(a != b) { integer index = 0; do { string chara = llGetSubString(a,index,index); string charb = llGetSubString(b,index,index); integer posa = llSubStringIndex(characters,chara); integer posb = llSubStringIndex(characters,charb); if((posa >= 0) && (posb >= 0)) { result = posa - posb; } else if(posa >= 0) { result = STRGREATERTHAN; } else if(posb >= 0) { result = STRLESSTHAN; } if(result != STREQUAL) index = len; ++index; } while(index < len); } // limit to constant values if(result < STRLESSTHAN)result = STRLESSTHAN; else if(result > STRGREATERTHAN)result = STRGREATERTHAN; return result; }
integer strnicmp(string a, string b,integer len) { string lca = llToLower(a); string lcb = llToLower(b); return strncmp(lca,lcb,len); }
integer strcmp(string a, string b) { integer lena = llStringLength(a); integer lenb = llStringLength(b); integer result = STRINVALID; if(lena < lenb) result = strncmp(a,b,lena); else result = strncmp(a,b,lenb); return result; }
integer stricmp(string a, string b) { integer lena = llStringLength(a); integer lenb = llStringLength(b); string lca = llToLower(a); string lcb = llToLower(b); integer result = STRINVALID; if(lena < lenb) result = strncmp(lca,lcb,lena); else result = strncmp(lca,lcb,lenb); return result; }
default { state_entry() { string a = "ABCDEF[]"; string b = "abcdef[]"; integer result = strcmp(a,b); integer result2 = strncmp(a,b,3); integer result3 = stricmp(a,b); integer result4 = strnicmp(a,b,4); llOwnerSay("result = " + (string)result); llOwnerSay("result2 = " + (string)result2); llOwnerSay("result3 = " + (string)result3); llOwnerSay("result4 = " + (string)result4); } } For non C programmers the functions are strcmp(a,b) - compare every character strncmp(a,b,n) - compare only first n characters stricmp(a,b) - case insensitive compare strnicmp(a,b,n) - case in sensitive first n compare
|