Difference between revisions of "ListUnique"

From Second Life Wiki
Jump to navigation Jump to search
m
Line 38: Line 38:
Originated in a November 2004 thread led by Chromal Brodsky in the SL Scripting Forum. http://forums-archive.secondlife.com/54/30/28137/1.html
Originated in a November 2004 thread led by Chromal Brodsky in the SL Scripting Forum. http://forums-archive.secondlife.com/54/30/28137/1.html


<div>


A *possibly* faster replacement (at least in my experiments in removing duplicate Agent UUIDs from multiple scanners on a sim):
<lsl>
list nvStripDupes(list DupedList)
{
    integer pos;
    while (llGetListLength(DupedList) != 0)
    {
        Element = llList2List(DupedList, 0, 0);
        NoDupes += Element;
        pos = llListFindList(DupedList, Element);
        while ( pos != -1)
        {
            DupedList = llDeleteSubList(DupedList, pos, pos);
            pos = llListFindList(DupedList, Element);
        }
    }
    return NoDupes;
}
</lsl>
{{LSLC|Examples|ListUnique}}
{{LSLC|Examples|ListUnique}}
[[Category:LSL_User-Defined Functions]]
[[Category:LSL_User-Defined Functions]]

Revision as of 22:30, 28 June 2011

Function: list ListUnique(list lAll);

Given a list of elements, strips out any duplicates in that list, and returns the de-duped list.

See also: Lists


<lsl> list ListUnique( list lAll ) {

   integer i;
   list lFiltered = llList2List(lAll, 0, 0);
   integer iAll = llGetListLength( lAll );
   for (i = 1; i < iAll; ++i) {
       if ( llListFindList(lFiltered, llList2List(lAll, i, i) ) == -1 ) {
           lFiltered += llList2List(lAll, i, i);
       }
   }
   return lFiltered;

}

</lsl>


Example:

list mylist = ListUnique(["A", "A", "B", "C", "C", "B"])

would return the list:

["A", "B", "C"]


Originated in a November 2004 thread led by Chromal Brodsky in the SL Scripting Forum. http://forums-archive.secondlife.com/54/30/28137/1.html

A *possibly* faster replacement (at least in my experiments in removing duplicate Agent UUIDs from multiple scanners on a sim):

<lsl> list nvStripDupes(list DupedList) {

   integer pos;
   while (llGetListLength(DupedList) != 0)
   {
       Element = llList2List(DupedList, 0, 0);
       NoDupes += Element;
       pos = llListFindList(DupedList, Element);
       while ( pos != -1)
       {
           DupedList = llDeleteSubList(DupedList, pos, pos);
           pos = llListFindList(DupedList, Element);
       }
   }
   return NoDupes;

} </lsl>