Difference between revisions of "ListXnotY"

From Second Life Wiki
Jump to navigation Jump to search
(You should initialize the variable when you declare it, as the compiler will implicitly initialize it. Implicit initialization isn't as readable as explicit, which is what examples should be: readable)
m (<lsl> tag to <source>)
 
(10 intermediate revisions by 7 users not shown)
Line 1: Line 1:
{{LSL Header}} __NOTOC__
{{LSL Header}} __NOTOC__
[[Category:LSL_User-Defined Functions]]
<div id="box">
<div id="box">
== Function: [[list]] ListXnotY([[list]] {{LSL Param|lx}}, [[list]] {{LSL Param|ly}}); ==
== Function: [[list]] ListXnotY([[list]] {{LSL Param|lx}}, [[list]] {{LSL Param|ly}}); ==
Line 7: Line 8:
See also: [[List|Lists]]
See also: [[List|Lists]]


<lsl>
<source lang="lsl2">
list ListXnotY(list lx, list ly) {// return elements in X list that are not in Y list
list ListXnotY(list lx, list ly) {// return elements in X list that are not in Y list
     list lz = [];
     list lz;
     integer i = 0;
     integer i = llGetListLength(lx);
    integer n = llGetListLength(lx);
     while(i--)
     for (; i < n; i++)
if ( !~llListFindList(ly,llList2List(lx,i,i)) ) // * see Note
    {
            lz += llList2List(lx,i,i);
        if (llListFindList(ly,llList2List(lx,i,i)) == -1)
        lz = lz + llList2List(lx,i,i);
    }
     return lz;
     return lz;
}
}


</lsl>
</source>
 
Note: If the if test is written as '''if (!~llListFindList''' ... , the result will be a list of elements in X that are ''missing'' from list Y. If it is written as '''if (~llListFindList''' ... , the result will be a list of elements in X that are ''present'' in list Y.


Example:<br />
Example:<br />


<lsl>
<source lang="lsl2">
list l1 = ["a","b","c","d"];
list l1 = ["a","b","c","d"];
list l2 = ["a","1","b","2","c","3"];
list l2 = ["a","1","b","2","c","3"];
list ListXnotY(list lx, list ly) {// return elements in X list that are not in Y list
    list lz;
    integer i = llGetListLength(lx);
    while(i--)
if ( !~llListFindList(ly,llList2List(lx,i,i)) )
            lz += llList2List(lx,i,i);
    return lz;
}


default{
default{
Line 35: Line 42:
   }
   }
}
}
</lsl>
</source>


</div>
</div>


Posted with the kind permission of Very Keynes, who originated this script June 2007 in the SL scripters forum http://forums.secondlife.com/showthread.php?t=194138
Posted with the kind permission of Very Keynes, who originated this script June 2007 in the SL scripters forum http://forums-archive.secondlife.com/54/e4/194138/1.html


{{LSLC|Examples|ListXnotY}}
{{LSLC|Examples|ListXnotY}}

Latest revision as of 15:25, 22 January 2015

Function: list ListXnotY(list lx, list ly);

Show what x list has that y list is missing.

See also: Lists

list ListXnotY(list lx, list ly) {// return elements in X list that are not in Y list
    list lz;
    integer i = llGetListLength(lx);
    while(i--)
	if ( !~llListFindList(ly,llList2List(lx,i,i)) )  // * see Note
            lz += llList2List(lx,i,i);
    return lz;
}

Note: If the if test is written as if (!~llListFindList ... , the result will be a list of elements in X that are missing from list Y. If it is written as if (~llListFindList ... , the result will be a list of elements in X that are present in list Y.

Example:

list l1 = ["a","b","c","d"];
list l2 = ["a","1","b","2","c","3"];

list ListXnotY(list lx, list ly) {// return elements in X list that are not in Y list
    list lz;
    integer i = llGetListLength(lx);
    while(i--)
	if ( !~llListFindList(ly,llList2List(lx,i,i)) )
            lz += llList2List(lx,i,i);
    return lz;
}

default{
   state_entry() {
      llSay(0, "X list has this in it, but Y list doesn't: " + llList2CSV(ListXnotY(l1,l2)) );
      //will say: d
   }
}

Posted with the kind permission of Very Keynes, who originated this script June 2007 in the SL scripters forum http://forums-archive.secondlife.com/54/e4/194138/1.html