Difference between revisions of "ListXxorY"

From Second Life Wiki
Jump to navigation Jump to search
m (Added extra example.)
 
(7 intermediate revisions by 5 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:
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.
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: [[List|Lists]]


<lsl>
 
<source lang="lsl2">
list ListXxorY(list lx, list ly) { // return elements that are in X or Y but not both
list ListXxorY(list lx, list ly) { // return elements that are in X or Y but not both
     return ListXnotY( lx, ly) + ListXnotY( ly, lx);
     return ListXnotY( lx, ly) + ListXnotY( ly, lx);
}
}
</lsl>
</source>




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 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{
default{
Line 27: Line 44:
   }
   }
}
}
</lsl>
</source>
 
Example 2 (Getting items added/removed from an inventory):<br />
<source lang="lsl2">
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.
        }
    }
}
</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 22:22, 5 December 2018

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