ListCompare: Difference between revisions

From Second Life Wiki
Jump to navigation Jump to search
m Wanted to add that the == 0 is not required, as the logical negation of the function means that any non-zero value will evaluate to FALSE, while /only/ zero will evaluate to TRUE.
m <lsl> tag to <source>
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{LSL_Function
{{LSL_Function
|func=ListCompare
|func=ListCompare
|func_desc=Efficiently compares two lists for equality (same contents).
|func_desc=Efficiently compares two lists for equality (same contents in the same order).
|return_type=integer
|return_type=integer
|return_text=TRUE if the lists are equal, FALSE if not.
|return_text=TRUE if the lists are equal, FALSE if not.
Line 9: Line 9:
|cat1=Examples
|cat1=Examples
|cat2=Library
|cat2=Library
|examples=<lsl>integer r = ListCompare([1, 2.0, "3"], [1, 2.0, "3"]); // Returns TRUE
|cat3=User-Defined Functions
|examples=<source lang="lsl2">integer r = ListCompare([1, 2.0, "3"], [1, 2.0, "3"]); // Returns TRUE
integer s = ListCompare([1, 2.0, 3], [1, 2.0, "3"]);  // Returns FALSE
integer s = ListCompare([1, 2.0, 3], [1, 2.0, "3"]);  // Returns FALSE
integer t = ListCompare([], [1, 2.0, "3"]);            // Returns FALSE</lsl>
integer t = ListCompare([], [1, 2.0, "3"]);            // Returns FALSE</source>
}}
}}


= Implementation =
= Implementation for LSO or MONO =
<lsl>integer ListCompare(list a, list b) {
<source lang="lsl2">
     integer aL = a != [];
integer ListCompare(list a, list b)  
     if (aL != (b != [])) return 0;
{
     if (aL == 0)         return 1;
     if (a != b)        return FALSE;   // Note: This is comparing list lengths only
   
 
     return !llListFindList((a = []) + a, (b = []) + b);
    // The next line is only needed if compiling with LSO
}</lsl>
     if (a == [])       return TRUE;    // both lists are empty (as both lists are the same length)
 
     return !llListFindList(a, b)
    // As both lists are the same length, llListFindList() can only return 0 or -1
    // Which we return as TRUE or FALSE respectively   
}
</source>

Latest revision as of 15:18, 22 January 2015

Summary

Function: integer ListCompare( list a, list b );

Efficiently compares two lists for equality (same contents in the same order).
Returns an integer TRUE if the lists are equal, FALSE if not.

• list a The first list to compare.
• list b The second list to compare.

Examples

<source lang="lsl2">integer r = ListCompare([1, 2.0, "3"], [1, 2.0, "3"]); // Returns TRUE integer s = ListCompare([1, 2.0, 3], [1, 2.0, "3"]); // Returns FALSE

integer t = ListCompare([], [1, 2.0, "3"]); // Returns FALSE</source>

Implementation for LSO or MONO

<source lang="lsl2"> integer ListCompare(list a, list b) {

   if (a != b)        return FALSE;    // Note: This is comparing list lengths only
   // The next line is only needed if compiling with LSO
   if (a == [])       return TRUE;     // both lists are empty (as both lists are the same length)
   return !llListFindList(a, b);  
   // As both lists are the same length, llListFindList() can only return 0 or -1 
   // Which we return as TRUE or FALSE respectively    

} </source>