Difference between revisions of "String Compare"

From Second Life Wiki
Jump to navigation Jump to search
(Brushed up and added to category)
Line 25: Line 25:
</lsl>
</lsl>


=== Notes: ===
I'm not sure why the string length comparison is in above function, it is not needed. The concept is nice though. I've modified this function. Given strings s1 and s2, it'l return 1 if s1>s2, -1 if s1<s2 and 0 if they are equal:
<lsl>
integer compare(string s1, string s2) {
  if (s1 == s2) {
        return 0;
    } else {
        list l = [s1, s2];
        l = llListSort(l, 1, FALSE);
        if (s1 == llList2String(l, 0)) //s1>s2
            return 1;
        return -1;
    }
}
</lsl>
{{LSLC|Library}}
{{LSLC|Library}}
{{LSLC|Examples}}
{{LSLC|Examples}}

Revision as of 04:46, 17 October 2009

String compare

Created by Xaviar Czervik. Do whatever you wish with this function: Sell it (good luck), use it, or modify it.

The following code returns 1, -1 or 0. The 1 and -1 are essentially random, however it will return the same value every time the script is executed. 0 Is returned when, and only when, the strings are exactly equal. Completely re-designed to use a few tricks I learned in the past months. Also much easier to read now.

I have used this script for comparing the Keys of two objects in a dynamic set of sensors, to decide which one should be the "Master" in the region to tell me information. I'm sure there can be more uses to this...

<lsl> integer compare(string s1, string s2) {

   if (s1 == s2) {
       return 0;
   } else if (llStringLength(s1) < llStringLength(s2)) {
       return 1;
   } else if (llStringLength(s1) > llStringLength(s2)) {
       return -1;
   } else {
       list l = [s1, s2];
       l = llListSort(l, 0, 1);
       if (s1 == llList2String(l, 0))
           return 1;
       return -1;
   }

} </lsl>

Notes:

I'm not sure why the string length comparison is in above function, it is not needed. The concept is nice though. I've modified this function. Given strings s1 and s2, it'l return 1 if s1>s2, -1 if s1<s2 and 0 if they are equal: <lsl> integer compare(string s1, string s2) {

  if (s1 == s2) {
       return 0;
   } else {
       list l = [s1, s2];
       l = llListSort(l, 1, FALSE);
       if (s1 == llList2String(l, 0)) //s1>s2
           return 1;
       return -1;
   }

} </lsl>