ListXxorY
Jump to navigation
Jump to search
LSL Portal | Functions | Events | Types | Operators | Constants | Flow Control | Script Library | Categorized Library | Tutorials |
Function: list ListXnotY(list lx, list ly);
Returns a new list, composed of elements that were in either original script, but not both.
Note: This is not the same as getting rid duplicates by preserving just one of each duplicated item. It goes further, and removes both items.
See also: Lists
list ListXxorY(list lx, list ly) { // return elements that are in X or Y but not both
return ListXnotY( lx, ly) + ListXnotY( ly, lx);
}
Example:
list l1 = ["a","b","c","d"];
list l2 = ["a","1","b","2","c","3"];
list ListXxorY(list lx, list ly) { // return elements that are in X or Y but not both
return ListXnotY( lx, ly) + ListXnotY( ly, lx);
}
list ListXnotY(list lx, list ly) {// return elements in X list that are not in Y list
list lz = [];
integer i = 0;
integer n = llGetListLength(lx);
for (; i < n; i++)
if (llListFindList(ly,llList2List(lx,i,i)) == -1)
lz += llList2List(lx,i,i);
return lz;
}
default{
state_entry() {
llSay(0, "Elements that are in x list or y list but not both: " + llList2CSV(ListXxorY(l1,l2)));
//will say: d, 1, 2, 3
}
}
Example 2 (Getting items added/removed from an inventory):
list ListXxorY(list lx, list ly) { // return elements that are in X or Y but not both
return ListXnotY( lx, ly) + ListXnotY( ly, lx);
}
list ListXnotY(list lx, list ly) {// return elements in X list that are not in Y list
list lz = [];
integer i = 0;
integer n = llGetListLength(lx);
for (; i < n; i++)
if (llListFindList(ly,llList2List(lx,i,i)) == -1)
lz += llList2List(lx,i,i);
return lz;
}
list getInventory() {//method for returning a list of all inventory items in a list.
list InventoryList;
integer count = llGetInventoryNumber(INVENTORY_ALL);
string ItemName;
while (count--)
{
ItemName = llGetInventoryName(INVENTORY_ALL, count);
if (ItemName != llGetScriptName() )
InventoryList += ItemName;
}
return InventoryList;
}
list saved_inventory_list;
default
{
state_entry()
{
saved_inventory_list = getInventory(); //Grab the list at state_entry to compare in future changes.
}
changed(integer change)
{
if(change & CHANGED_INVENTORY)
{
list new_inventory_list = getInventory();
list xory = ListXxorY(new_inventory_list,saved_inventory_list); //Comparing new inventory with old one.
string item_changed = llList2String(xory, 0); //xory list will always be a size of 1 in changed event.
//If the new inventory is greater than the saved inventory then it means an item hs been added, otherwise removed.
if(llGetListLength(new_inventory_list) > llGetListLength(saved_inventory_list))
{
llOwnerSay("Item Added: " + item_changed);
}else
{
llOwnerSay("Item Removed: " + item_changed);
}
saved_inventory_list = new_inventory_list; //Be sure to update the saved inventory list.
}
}
}
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